![](/img/trans.png)
[英]Using RCP or FTP to copy files from a remote unix machine onto a local windows machine
[英]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.