2014-05-20 37 views
0

我有一個客戶端 - 服務器應用程序。服務器應用程序在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(); 
      }    
     } 

回答

0

檢查文件夾是否爲空。如果它是空的,請在您的客戶端上創建一個新文件夾。一個簡單的解決辦法:)

+0

謝謝你,我在做你喜歡的東西,但我仍然想發送一個FileEvent對象的路徑。這是錯誤的,我會簡單地發送路徑,所以客戶端將創建空文件夾。 – crstna09

+0

是的..你是絕對正確的:) –

1

爲什麼不通過客戶端代碼創建空文件夾?

0

喜歡的東西:

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 

應該工作。

+0

這也是一個好主意,但對我的應用程序實現起來有點困難。謝謝。 – crstna09

相關問題