我正在尝试使用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,我很确定这意味着连接没有通过代理。
如何使用此连接类型将用户凭据传递给代理。
BTW,我可以使用以下代码通过相同的代理成功创建URLConnection;是否有Apache FTPClient的等价物?
conn = url.openConnection();
String password = "username:password";
String encodedPassword = new String(Base64.encodeBase64(password.getBytes()));
conn.setRequestProperty("Proxy-Authorization", encodedPassword);
答案 0 :(得分:2)
如果要使用代理,请使用FTPHTTPClient;
if(proxyHost !=null) {
System.out.println("Using HTTP proxy server: " + proxyHost);
ftp = new FTPHTTPClient(proxyHost, proxyPort, proxyUser, proxyPassword);
}
else {
ftp = new FTPClient();
}
答案 1 :(得分: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("...", "...");
}