【问题标题】:File Copy from local to remote system using java IO使用 java IO 将文件从本地复制到远程系统
【发布时间】:2015-11-27 04:20:12
【问题描述】:

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

    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)

【问题讨论】:

    标签: java file io copy


    【解决方案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");
    

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

    【讨论】:

    • 谢谢,问题已解决,但我收到拒绝访问异常,尽管我正在复制的文件夹有权写入文件夹@Ben N
    • @jainesh 是的,机器不只是让任何人进来并在它们的任何地方写字。您需要进行一些研究并配置适当的安全设置。
    • 已设置安全设置以允许在文件夹@Ben N 上写入
    • 嗨,您能否发布有关如何设置安全/凭据设置以访问 VM 的代码。
    • @GauravKhandelwal 您需要使用标准的 Windows 安全 ACL 编辑器手动完成。 (如果远程机器不允许远程写入,几乎可以肯定它也被配置为不允许更改权限。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 2013-08-13
    • 2020-07-18
    相关资源
    最近更新 更多