用Java在网络上创建文件夹
我想在网络上发送文件夹,根据我的代码,如果文件夹包含文件,它会成功发送,但如果文件夹包含在文件夹内,它不会在客户端创建文件夹 服务器代码:用Java在网络上创建文件夹,java,stream,filestream,Java,Stream,Filestream,我想在网络上发送文件夹,根据我的代码,如果文件夹包含文件,它会成功发送,但如果文件夹包含在文件夹内,它不会在客户端创建文件夹 服务器代码: ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream()); File file = new File("home/"); File[] children = file.listFiles(); if (children != null) { for (Fi
ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
File file = new File("home/");
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
all.add(child);
if(!child.isDirectory()){
oos.writeObject(child.getName());
FileInputStream fis = new FileInputStream(child);
while ((bytesRead = fis.read(buffer)) > 0) {
oos.writeObject(bytesRead);
oos.writeObject(Arrays.copyOf(buffer, buffer.length));
}
}
}
}
客户端代码:
oos = new ObjectOutputStream(theSocket.getOutputStream());
ois = new ObjectInputStream(theSocket.getInputStream());
out = new PrintWriter(theSocket.getOutputStream( ));
while (true) {
Object o = ois.readObject();
File file = new File(o.toString());
if(file.isDirectory())
File Dir = new File("new/").mkdir();
if(!file.isDirectory()){
FileOutputStream fos = new FileOutputStream(o.toString());
do {
o = ois.readObject();
bytesRead = (Integer) o;
o = ois.readObject();
buffer = (byte[])o;
fos.write(buffer, 0, bytesRead);
}
while (bytesRead == BUFFER_SIZE);
fos.close();
}
}
它不会显示任何错误,而是在客户端创建一个名为(服务器端的文件夹)的匿名文件。请告诉我我的代码有什么问题 我认为你的答案(部分)是递归。有几种方法可以做到这一点。一种方法是将bytesRead作为-1或NULL发送给目录,并在客户端递归,另一种方法是发送文件的相对路径,并根据需要在客户端创建目录,我将在下面介绍(效率有点低)
服务器端:
void processFile(File file, String path)
{
if (file.isDirectory())
{
File[] children = file.listFiles();
for (File child : children)
{
all.add(child);
processFile(child, path + file.getName() + "/");
}
}
else
{
oos.writeObject(path + child.getName());
FileInputStream fis = new FileInputStream(child);
while ((bytesRead = fis.read(buffer)) > 0)
{
oos.writeObject(bytesRead);
oos.writeObject(buffer);
}
oos.writeObject(BUFFER_SIZE);
}
}
while (true)
{
String name = (String)ois.readObject();
if (name.length() == 0) break;
// "/new/" is the absolute path of the directory where you want all these files to be created
name += "/new/";
File dir = new File(name.substring(0, name.lastIndexOf('/')));
if (!dir.exists())
dir.mkdirs();
FileOutputStream fos = new FileOutputStream(new File(name));
bytesRead = (Integer) ois.readObject();
while (bytesRead != BUFFER_SIZE)
{
buffer = (byte[])ois.readObject();
fos.write(buffer, 0, bytesRead);
bytesRead = (Integer) ois.readObject();
}
fos.close();
}
使用processFile(文件“”)调用
或processFile(文件“/”
)
在调用后说类似于oos.writeObject(“”)的话,并在客户端执行类似于name.length()==0的检查,以知道何时停止
客户端:
void processFile(File file, String path)
{
if (file.isDirectory())
{
File[] children = file.listFiles();
for (File child : children)
{
all.add(child);
processFile(child, path + file.getName() + "/");
}
}
else
{
oos.writeObject(path + child.getName());
FileInputStream fis = new FileInputStream(child);
while ((bytesRead = fis.read(buffer)) > 0)
{
oos.writeObject(bytesRead);
oos.writeObject(buffer);
}
oos.writeObject(BUFFER_SIZE);
}
}
while (true)
{
String name = (String)ois.readObject();
if (name.length() == 0) break;
// "/new/" is the absolute path of the directory where you want all these files to be created
name += "/new/";
File dir = new File(name.substring(0, name.lastIndexOf('/')));
if (!dir.exists())
dir.mkdirs();
FileOutputStream fos = new FileOutputStream(new File(name));
bytesRead = (Integer) ois.readObject();
while (bytesRead != BUFFER_SIZE)
{
buffer = (byte[])ois.readObject();
fos.write(buffer, 0, bytesRead);
bytesRead = (Integer) ois.readObject();
}
fos.close();
}
你确定你需要这个斜线吗new File(“new/”).mkdir()
是的,没有问题,只是用它在同一个目录中创建文件夹对不起,我只是想澄清一下您试图解释的内容:“我想在网络上发送文件夹”=您想通过网络将文件对象的实例(目录)发送到给定的目标。“根据我的代码,如果文件夹中包含文件,它会成功地将其发送。”。。你在使用一个无止境的循环吗?为什么不创建一个计划任务来不时验证此文件夹?(). “但若文件夹中包含文件夹,则不会在客户端创建文件夹?”。。什么?对不起,我不能理解这个问题。它不是序列化就是java.io。行为。这似乎与网络无关。这个问题一点也不清楚。但是,有很多不相关的代码-1@theMarceloR,文件夹内的文件已成功发送,文件流的bcz,但当文件夹在文件夹内继续时,如何向客户端发送该文件夹为内部文件夹的指示?