繁体   English   中英

使用Java将文件从本地窗口机器复制到远程Windows机器

[英]Copy files from local window machine to remote windows machine using java

我进行了很多搜索,但无法为此找到解决方案。 我需要使用Java程序将文件从本地Windows计算机复制到远程Windows计算机。 我已经尝试过JSch,

JSch jsch = new JSch();
    Session session = null;
    session = jsch.getSession("username","hostname",22);
    session.setPassword("password");
    session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
    ChannelSftp channel = null;
    channel = (ChannelSftp)session.openChannel("sftp");
    channel.connect();
        File localFile = new File("filePath");
        //If you want you can change the directory using the following line.
        channel.cd("E:/xxx");
    channel.put(new FileInputStream(localFile),localFile.getName());
        channel.disconnect();
    session.disconnect();

在执行上述代码时,我面临以下错误,

Exception in thread "main" 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)

我在远程Windows机器上安装了cygwin。 看来Jsch找不到Windows路径。 将文件从Windows计算机复制到Linux计算机时,相同的代码可以正常工作。

请给我一个以上问题的解决方案,或者在Java中是否有其他选择可以实现? 谢谢

为了使用驱动器号解析Windows路径,您可能需要使用/cygdrive前缀 在这种情况下,应该使用参数/cygdrive/e/xxx来调用cd方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM