2012-08-22 28 views
1

我想從服務器下載整個文件夾/目錄。 該文件夾包含文件。我試着用zip功能,但爲此我需要給路徑,直到文件,而不是文件夾路徑。如何在java中下載整個文件夾?

像 -

BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\StoreFiles\\Temp\\profile.txt")); 

我想是這樣( 「d:\ StoreFiles」),將下載Storefiles文件夾中的所有文件夾和文件夾內的文件。

我該如何實現?

+2

我看看在Apache Commons IO'FileUtils.copyDirectory' – MadProgrammer

回答

0

我覺得這個例子ü

public class CopyDirectoryExample 
{ 
    public static void main(String[] args) 
    { 
     File srcFolder = new File("c:\\mkyong"); 
     File destFolder = new File("c:\\mkyong-new"); 

     //make sure source exists 
     if(!srcFolder.exists()){ 

      System.out.println("Directory does not exist."); 
      //just exit 
      System.exit(0); 

     }else{ 

      try{ 
      copyFolder(srcFolder,destFolder); 
      }catch(IOException e){ 
      e.printStackTrace(); 
      //error, just exit 
       System.exit(0); 
      } 
     } 

     System.out.println("Done"); 
    } 

    public static void copyFolder(File src, File dest) 
     throws IOException{ 

     if(src.isDirectory()){ 

      //if directory not exists, create it 
      if(!dest.exists()){ 
       dest.mkdir(); 
       System.out.println("Directory copied from " 
           + src + " to " + dest); 
      } 

      //list all the directory contents 
      String files[] = src.list(); 

      for (String file : files) { 
       //construct the src and dest file structure 
       File srcFile = new File(src, file); 
       File destFile = new File(dest, file); 
       //recursive copy 
       copyFolder(srcFile,destFile); 
      } 

     }else{ 
      //if file, then copy it 
      //Use bytes stream to support all file types 
      InputStream in = new FileInputStream(src); 
       OutputStream out = new FileOutputStream(dest); 

       byte[] buffer = new byte[1024]; 

       int length; 
       //copy the file content in bytes 
       while ((length = in.read(buffer)) > 0){ 
        out.write(buffer, 0, length); 
       } 

       in.close(); 
       out.close(); 
       System.out.println("File copied from " + src + " to " + dest); 
     } 
    } 
} 
+2

嗨,請複製其他人的代碼時,始終提供歸屬地。這是必需的,而且這是優雅的事情。 http://www.mkyong.com/java/how-to-copy-directory-in-java/ –

1

這個怎麼樣有用嗎?它遞歸地進入目錄並下載:

public static void main(String[] args) { 
     directoryDownloader(new File("/Users/eugene/Desktop")); 
    } 

    private static void directoryDownloader(File input){ 
     if(input.isDirectory()){ 
      for(File file : input.listFiles()){ 
       directoryDownloader(file); 
      } 
     } else { 
      downloadFile(input); 
     } 
} 

private static void downloadFile(File someFile){ 
    System.out.println("Downloading file : " + someFile.getPath()); 
} 

P.S.實現你想要的downloadFile。

+0

謝謝MadProgrammer,Sujay,aravindKrishna,Eugene的建議和例子,真的幫了很多! – sumitg

+0

@sumitg upvote是這裏最好的謝謝你;) – Eugene

2

我會建議看看Apache Commons IO FileUtils複製目錄。這很容易使用。 Have a look at the javadoc

一些有用的方法,可能會派上用場(注意,有幾個可用的那些):

  • copyDirectory(File srcDir, File destDir)
  • copyDirectory(File srcDir, File destDir, FileFilter filter
相關問題