Java 如何将空文件夹从服务器复制到客户端
我有一个客户机-服务器应用程序。服务器应用程序在Ubuntu机器上,客户端应用程序在Windows上。当我启动应用程序时,要做的第一件事是将特定文件夹从服务器复制到客户端的特定位置。问题是我无法通过套接字发送空文件夹,因为通过套接字发送的对象是ar fileEvent对象。有人知道如何发送空文件夹吗?我附加了从服务器向客户端发送文件的功能Java 如何将空文件夹从服务器复制到客户端,java,file,client-server,Java,File,Client Server,我有一个客户机-服务器应用程序。服务器应用程序在Ubuntu机器上,客户端应用程序在Windows上。当我启动应用程序时,要做的第一件事是将特定文件夹从服务器复制到客户端的特定位置。问题是我无法通过套接字发送空文件夹,因为通过套接字发送的对象是ar fileEvent对象。有人知道如何发送空文件夹吗?我附加了从服务器向客户端发送文件的功能 public void locateFiles(String s,String d) throws IOExcepti
public void locateFiles(String s,String d) throws IOException {
File srcDir = new File(s);
if (!srcDir.isDirectory()) {
System.out.println("Source directory is not valid ..Exiting the client");
System.exit(0);
}
File[] files = srcDir.listFiles();
fileCount = files.length;
if (fileCount == 0) {
sendFile(srcDir.getAbsolutePath()/*, fileCount - i - 1*/,d,s);
}
for(File f : files){
if(f.isDirectory())
{
locateFiles(f.toString(),d+f.getName()+"/");
}
else {
sendFile(f.getAbsolutePath()/*, fileCount - i - 1*/,d,s);
}
}
}
public void sendFile(String fileName,String desti,String su) {
fileEvent = new FileEvent();
fileEvent.setDestinationDirectory(desti);
fileEvent.setSourceDirectory(su);
File file = new File(fileName);
fileEvent.setFilename(file.getName());
DataInputStream diStream = null;
try {
diStream = new DataInputStream(new FileInputStream(file));
long len = (int) file.length();
byte[] fileBytes = new byte[(int) len];
int read = 0;
int numRead = 0;
while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
fileBytes.length - read)) >= 0) {
read = read + numRead;
}
fileEvent.setFileData(fileBytes);
fileEvent.setStatus("Success");
diStream.close();
} catch (Exception e) {
e.printStackTrace();
fileEvent.setStatus("Error");
}
sendFlag();
sendDest(desti);
try {
outputStream.writeObject(fileEvent);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void locateFiles(字符串s、字符串d)引发IOException{
文件srcDir=新文件;
如果(!srcDir.isDirectory()){
System.out.println(“源目录无效..正在退出客户端”);
系统出口(0);
}
File[]files=srcDir.listFiles();
fileCount=files.length;
if(fileCount==0){
sendFile(srcDir.getAbsolutePath()/*,fileCount-i-1*/,d,s);
}
用于(文件f:文件){
if(f.isDirectory())
{
locateFiles(f.toString(),d+f.getName()+“/”;
}
否则{
sendFile(f.getAbsolutePath()/*,fileCount-i-1*/,d,s);
}
}
}
public void sendFile(字符串文件名、字符串desti、字符串su){
fileEvent=newfileevent();
fileEvent.setDestinationDirectory(desti);
fileEvent.setSourceDirectory(su);
文件=新文件(文件名);
fileEvent.setFilename(file.getName());
DataInputStream diStream=null;
试一试{
diStream=新数据输入流(新文件输入流(文件));
long len=(int)file.length();
字节[]fileBytes=新字节[(int)len];
int read=0;
int numRead=0;
而(read=0){
读取=读取+numRead;
}
fileEvent.setFileData(fileBytes);
fileEvent.setStatus(“成功”);
diStream.close();
}捕获(例外e){
e、 printStackTrace();
fileEvent.setStatus(“错误”);
}
sendFlag();
sendDest(desti);
试一试{
outputStream.writeObject(fileEvent);
outputStream.flush();
}捕获(IOE异常){
e、 printStackTrace();
}
}
为什么不使用客户端代码创建空文件夹?检查文件夹是否为空。如果为空,请在客户端上创建一个新文件夹。一个简单的解决方法:)类似于:
if (folder is empty) then
create temporary file in folder
temp_file_created <- true
endif
copy folder to destination
if (temp_file_created) then
delete temp file from source machine
delete temp file from destination machine
temp_file_created <- false
endif
如果(文件夹为空),则
在文件夹中创建临时文件
temp_file_创建谢谢,我正在做一些类似于您所建议的事情,但我仍然尝试将路径作为FileEvent对象发送。这是一个错误,我将简单地发送路径,因此客户端将创建空文件夹。这也是一个好主意,但在我的应用程序中实现有点困难。谢谢