我正在使用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
份額TransferOverFTPS.java – JRR
的代碼的整個碼是功能myTransform(),而不是僅粘貼主要功能:公共靜態無效的主要(字串[] args){ \t \t TransferOverFTPS測試=新TransferOverFTPS( ); \t \t test.myTransform(); \t} – Robert
你確定最後一個輸出是「連接到...」,而不是「登錄到FTP服務器失敗」,看起來好像ftpClient已關閉 – devpuh