使用 Java 通过代理进行 FTP 连接 [英] FTP connection through proxy with Java

查看:79
本文介绍了使用 Java 通过代理进行 FTP 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 org.apache.commons.net.ftp.FTPClient 通过代理连接到 FTP 服务器.非常确定系统属性设置正确,如下所示:

I'm trying to connect to an FTP server through a proxy using org.apache.commons.net.ftp.FTPClient. Pretty sure the system properties are getting set correctly as per following:

Properties props = System.getProperties();
props.put("ftp.proxySet", "true");
// dummy details
props.put("ftp.proxyHost", "proxy.example.server");
props.put("ftp.proxyPort", "8080");

创建连接会引发 UnknownHostException,我很确定这意味着连接没有通过代理.

Creating a connection raises a UnknownHostException which I'm pretty sure means the connection isn't making it past the proxy.

如何将用户凭据传递到具有这种连接类型的代理.

How can user credentials be passed through to the proxy with this connection type.

顺便说一句,我可以使用以下方法通过相同的代理成功创建一个 URLConnection;Apache FTPClient 是否有等价物?

BTW, I can successfully create a URLConnection through the same proxy using the following; is there an equivalent for the Apache FTPClient?

conn = url.openConnection();
String password = "username:password";
String encodedPassword = new String(Base64.encodeBase64(password.getBytes()));
conn.setRequestProperty("Proxy-Authorization", encodedPassword);

推荐答案

当你想使用代理时使用 FTPHTTPClient 怎么样;

How about using the FTPHTTPClient when you want to use a proxy;

if(proxyHost !=null) {
  System.out.println("Using HTTP proxy server: " + proxyHost);
  ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword);
}
else {
  ftp = new FTPClient();
} 

这篇关于使用 Java 通过代理进行 FTP 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆