Apache Commons FTPClient Loggin
问题描述:
我正在使用org.apache.commons.net.ftp.FTPSClient将文件上传到FTPS服务器(通过TLS/SSL)。Apache Commons FTPClient Loggin
请注意FTP服务器位于Windows服务器上,证书是自签名证书,可以连接并通过FileZilla成功上传文件,之后我接受证书无效的警告。
但是,处理相同内容的java代码不起作用。我已经在代码中单独检查了连接步骤和登录步骤,并且发现在连接步骤(ftps.connect(服务器,端口))后发生了错误 - 在控制台中打印了“连接到服务器:端口”日志。我很困惑getRemoteAddress应该在已经完成的连接步骤中处理。
String ftpsServer = "host";
int ftpsPort = 21;
String ftpsUser = "domain\username";
String ftpsPass = "password";
try{
FTPSClient ftpClient = new FTPSClient();
ftpClient.connect(ftpsServer,ftpsPort);
// check FTP connection
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)){
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
} else {
System.out.println("Connected to " + ftpsServer + ":" + ftpsPort + ".");
}
// check FTP login
if (ftpClient.login(ftpsUser, ftpsPass)){
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
System.out.println("Logged into FTP server successfully");
} else {
System.out.println("Failed log into FTP server");
ftpClient.logout();
ftpClient.disconnect();
}
File localFile = new File("C:/test/history.txt");
InputStream inputStream = new FileInputStream(localFile);
boolean uploaded = ftpClient.storeFile("/wwwroot/preview/history.txt", inputStream);
System.out.println("uploaded successful? " + uploaded);
inputStream.close();
ftpClient.logout();
ftpClient.disconnect();
}catch(Exception e){
LOG.error("Exception when merging Reminderlist is: ", e);
}
完整的日志在控制台打印的是:
Connected to 10.20.254.xx:21.
Failed log into FTP server
ERROR 2017-10-13 15:27:15,322 [main]
com.redwood.contentmanagement.UnzipFiles: Exception when merging
Reminderlist is:
java.lang.NullPointerException
at
org.apache.commons.net.SocketClient.getRemoteAddress(SocketClient.java:553) ~[commons-net-2.2.jar:2.2]
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:509) ~[commons-net-2.2.jar:2.2]
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:425) ~[commons-net-2.2.jar:2.2]
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1548) ~[commons-net-2.2.jar:2.2]
at com.redwood.contentmanagement.TransferOverFTPS.myTransform(TransferOverFTPS.java:55) ~[classes/:?]
at com.redwood.contentmanagement.TransferOverFTPS.main(TransferOverFTPS.java:110) ~[classes/:?]
非常感谢@ devpuh的答复,我已成功登录的用户名更改\至/后,但仍未能上传文件,并有失败的地方没有详细的信息,下面是完整的日志中控制台:
Connected to 10.20.254.10:21.
Logged into FTP server successfully
uploaded successful? false
答
对于原来的问题
您的登录凭据不正确,但您仍在尝试上载文件。
您必须处理不正确的登录。例如:
// check FTP login
if (ftpClient.login(ftpsUser, ftpsPass)){
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
System.out.println("Logged into FTP server successfully");
} else {
System.out.println("Failed log into FTP server");
ftpClient.logout();
ftpClient.disconnect();
return;
}
所编辑的问题
要找出为什么上传不工作,你可以从服务器获取最后的答复与getReplyString()
。例如:
boolean uploaded = ftpClient.storeFile("/wwwroot/preview/history.txt", inputStream);
if(!uploaded) {
System.err.println("Can't upload File! Reply from Server: " + ftpClient.getReplyString());
} else {
System.out.println("Upload successful");
}
PS:在FTPClient很少显示错误或警告消息,因此最好是一个PrintCommandListener
添加到调试。
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
份额TransferOverFTPS.java – JRR
的代码的整个码是功能myTransform(),而不是仅粘贴主要功能:公共静态无效的主要(字串[] args){ \t \t TransferOverFTPS测试=新TransferOverFTPS( ); \t \t test.myTransform(); \t} – Robert
你确定最后一个输出是“连接到...”,而不是“登录到FTP服务器失败”,看起来好像ftpClient已关闭 – devpuh