我想将文件传输到远程位置,并且必须为此使用代理服务器。我能够通过以下命令连接到 FTP 位置:
sftp -o "ProxyCommand /usr/bin/nc -X connect -x <proxy_host>:<proxy_port> %h %p" username@ftp_server:
但是我想自动化这个文件传输过程,并且我使用 JSch 进行 SFTP,代码片段如下:
String sourceFile = sourceDir + fileName;
JSch jsch = new JSch();
int port = Integer.parseInt(getFtpPort());
Session session = jsch.getSession(getUserName(), getHost(), port);
session.setConfig(STRICT_HOST_CHECKING, ANSWER);
session.setProxy(new ProxyHTTP(<proxy_host>, <proxy_port>));
session.setPassword(getPassword());
session.connect();
Channel channel = session.openChannel(FILE_PROTOCOL);
channel.connect();
sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(desDir);
File fileToTransfer = new File(sourceFile);
sftpChannel.put(new FileInputStream(fileToTransfer), fileName);
使用上面的代码我得到以下异常:
Caused by: com.jcraft.jsch.JSchException: ProxyHTTP: java.io.IOException
at com.jcraft.jsch.ProxyHTTP.connect(ProxyHTTP.java:158)
at com.jcraft.jsch.Session.connect(Session.java:210)
at com.jcraft.jsch.Session.connect(Session.java:162)
最佳答案
这对我有用。
JSch jsch = new JSch();
java.util.Properties config = new java.util.Properties();
Session session = jsch.getSession(RemoteUserName, RemoteIpAddr, RemotePortNo);
session.setPassword(RemotePassword);
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setProxy(new ProxyHTTP(ProxyName, ProxyPort));
session.connect();
关于java - 如何使用带有 JSch 库的代理传输文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21424237/