如何使用带有JSch库的代理传输文件 [英] How to transfer a file using a proxy with JSch library
本文介绍了如何使用带有JSch库的代理传输文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文件传输到远程位置,因此我必须为此使用代理服务器.我可以通过以下命令连接到FTP位置:
I want to transfer files to a remote location and I am bound to use a proxy server for that. I was able to connect to the FTP location via following command :
sftp -o "ProxyCommand /usr/bin/nc -X connect -x <proxy_host>:<proxy_port> %h %p" username@ftp_server:
但是我想自动化该文件传输过程,并且我将JSch用于SFTP,代码段如下:
But I want to automate this process of file transfer and I am using JSch for SFTP and snippet of code is below:
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);
使用上面的代码,我得到以下异常:
With above code I am getting following exception:
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)
推荐答案
这对我有用
ProxyHTTP proxy = new ProxyHTTP("192.168.10.1",80)
proxy.setUserPasswd("username","password");
session.setProxy(proxy);
这篇关于如何使用带有JSch库的代理传输文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文