【问题标题】:FTP connection through proxy with Java使用 Java 通过代理进行 FTP 连接
【发布时间】:2011-07-07 05:22:16
【问题描述】:

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

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

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

如何将用户凭据传递到具有此连接类型的代理。

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

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

【问题讨论】:

  • 我从 oracle java docs 查到的,ftp 代理主机名为:ftp.proxHost。不是ftp.proxyHost。没有y。我不知道为什么,因为我没有 ftp 代理,所以我无法测试它。 java networking and proxies

标签: java proxy ftp-client apache-commons-net


【解决方案1】:

当你想使用代理时,如何使用 FTPHTTPClient;

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

【讨论】:

    【解决方案2】:

    我认为你需要使用Authenticator

    private static class MyAuthenticator extends Authenticator {
        private String username;
        private String password;
        public MyAuthenticator(String username, String password) {
            super();
            this.username = username;
            this.password = password;
        }
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password.toCharArray());
        }
    }
    
    public static void main(String[] args) {
        Authenticator.setDefault(new MyAuthenticator("foo", "bar"));
        System.setProperty("...", "...");
    }
    

    【讨论】:

    • 非常感谢。尝试了这种方法并收到了完全相同的异常。决定尝试 it.sauronsoftware.ftp4j.FTPClient 并能够使用他们的 HTTPTunnelConnector 成功连接。
    • 啊 ...那是因为您的代理可能不是 FTP 代理,而是通过 HTTP 隧道传输 FTP 的 HTTP 代理。很高兴您找到了解决方案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多