我有一個客戶端 - 服務器應用程序。服務器應用程序在Ubuntu機器上,客戶端應用程序在Windows上。當我啓動我的應用程序時,首先要做的是將特定文件夾從服務器複製到客戶端的特定位置。問題是我無法通過套接字發送一個空文件夾,因爲對象通過套接字發送了一個fileEvent對象。有沒有人有任何想法如何發送一個空文件夾?我附加了從服務器發送文件到客戶端的功能。如何將空文件夾從服務器複製到客戶端
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();
}
}
謝謝你,我在做你喜歡的東西,但我仍然想發送一個FileEvent對象的路徑。這是錯誤的,我會簡單地發送路徑,所以客戶端將創建空文件夾。 – crstna09
是的..你是絕對正確的:) –