【发布时间】:2014-01-29 07:00:02
【问题描述】:
我想将文件传输到远程位置,我必须为此使用代理服务器。我能够通过以下命令连接到 FTP 位置:
sftp -o "ProxyCommand /usr/bin/nc -X connect -x <proxy_host>:<proxy_port> %h %p" username@ftp_server:
但我想自动化这个文件传输过程,我正在使用 JSch 进行 SFTP 和 sn-p 的代码如下:
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)
【问题讨论】:
-
你有想过这个吗?使用 HTTP 代理面临同样的问题
标签: java sftp file-transfer jsch http-proxy