简体   繁体   中英

Copy files from local window machine to remote windows machine using java

I have searched a lot but couldn't get a solution for this. I need to copy a file from local windows machine to remote windows machine using java program. I have tried with 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();

While executing the above code, i am facing the below error,

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)

I have cygwin installed in remote windows machine. It seems Jsch is not able to find the windows path. The same code works properly when copying files from windows machine to linux machine.

Please tel me a solution for the above problem or is there any other options for this to achieve in java ? Thanks

In order to resolve a Windows path with a drive letter you may need to use the /cygdrive prefix . In this case, your cd method call should be called with the parameter /cygdrive/e/xxx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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