我想在网络上发送文件夹,根据我的代码,如果文件夹包含成功发送它们的文件,但如果文件夹包含内部文件夹,它不会在客户端创建文件夹?
服务器代码:
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();
}
}
它不显示任何错误,而是在客户端创建一个名称为匿名的文件(服务器端的文件夹)。请告诉我我的代码有什么问题!