使用java IO从本地文件复制到远程系统

时间:2015-11-27 04:20:12

标签: java file io copy

我需要将文件从本地系统复制到远程系统,为此我使用以下代码:

    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 个答案:

答案 0 :(得分:2)

此行的文件名

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");

还要确保远程计算机上的安全设置已配置为允许您的帐户进行适当的访问。