2

我正在尝试使用 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);
4

2 回答 2

2

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

if(proxyHost !=null) {
  System.out.println("Using HTTP proxy server: " + proxyHost);
  ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword);
}
else {
  ftp = new FTPClient();
} 
于 2012-06-07T11:05:36.383 回答
0

我认为您需要使用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("...", "...");
}
于 2011-07-07T12:38:25.620 回答