2010-11-15 69 views
1

我需要一個示例代碼,通過網絡將文件複製到Java本地文件系統。 這是如何在Java中完成的?如何通過網絡將文件複製到Java中的LocalDrive

+3

你會寫你的自己的服務器呢?或者你是否複製了FTP,或者其他什麼? – aioobe 2010-11-15 18:46:19

+0

要添加到aioobe的點,您是否希望從「映射」網絡驅動器,一個文件服務器偵聽遠程套接字和端口上的文件複製? – anirvan 2010-11-15 18:57:16

回答

0

這裏是代碼,在本地文件系統複製文件

File fromfile = new File("file"); 
    File tofile = new File("../copiedfile"); 
    tofile.createNewFile(); 
    FileInputStream from = new FileInputStream(fromfile); 
    FileOutputStream to = new FileOutputStream(tofile); 
    byte [] buffer = new byte[4096]; 
    int bytesread; 
    while ((bytesread = from.read(buffer)) != -1) { 
     to.write(buffer, 0, bytesread); 
    } 

我想,如果你想通過網絡,你應該使用ObjectOutput和插座發送緩衝區拷貝文件