2015-11-27 34 views
0

我需要將文件從我的本地系統複製到遠程系統,爲了這個,我正在使用下面的代碼:文件複製使用Java IO

public class Autohost { 

    public static void main(String[] args) throws IOException { 
     InputStream in = new FileInputStream(new File(
       "C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war")); 

     File f = new File("10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war"); 
     f.createNewFile(); 
     OutputStream out = new FileOutputStream(f); 
     // Transfer bytes from in to out 
     byte[] buf = new byte[1024]; 
     int len; 

     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 



     } 
     in.close(); 
     out.close(); 


    } 
} 

但我得到以下錯誤:

 Exception in thread "main" java.io.IOException: The system cannot find the path specified 
     at java.io.WinNTFileSystem.createFileExclusively(Native Method) 
     at java.io.File.createNewFile(Unknown Source) 
     at com.autohost2.java.Autohost.main(Autohost.java:18) 

回答

1

在這條線

File f = new File("10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war"); 

文件名不是有效的UNC路徑。您需要兩個反斜槓(代碼中的四個)來發送遠程路徑的信號。修正版本:

File f = new File("\\\\10.87.74.191\\C$\\IVS_Code\\tomcat\\apache-tomcat-7.0.57\\webapps\\AutohostDemo1_1145.war"); 

還要確保在遠程計算機上的安全設置被配置爲允許您的帳戶適當的訪問權限。

+0

謝謝你解決了這個問題,但我得到了訪問被拒絕的異常,雖然我正在複製的文件夾有權限在文件夾中寫入@Ben N – jainesh

+0

@jainesh對,機器不會讓任何人來在其中寫入和寫入。你需要做一些研究並配置適當的安全設置。 –

+0

已將安全設置設置爲允許在文件夾上寫@Ben N – jainesh