報錯代碼:
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(1000 * 30);//設置連接超時時間
ftpClient.setControlEncoding("utf-8");//設置ftp字符集
ftpClient.enterLocalPassiveMode();//設置被動模式,文件傳輸端口設置
try {
//設置文件傳輸模式為二進制,可以保證傳輸的內容不會被改變
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.connect(host, port);
ftpClient.login(username, password);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
LOGGER.error("connect ftp {} failed", host);
ftpClient.disconnect();
return null;
}
LOGGER.info("replyCode==========={}", replyCode);
} catch (IOException e) {
LOGGER.error("connect fail ------->>>{}", e.getMessage());
return null;
}
return ftpClient;
解決辦法:
將 設置文件傳輸模式為二進制,可以保證傳輸的內容不會被改變 調整到登錄之后
即將 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 調整到 ftpClient.login(username, password); 后面
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(1000 * 30);//設置連接超時時間
ftpClient.setControlEncoding("utf-8");//設置ftp字符集
ftpClient.enterLocalPassiveMode();//設置被動模式,文件傳輸端口設置
try {
ftpClient.connect(host, port);
ftpClient.login(username, password);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
LOGGER.error("connect ftp {} failed", host);
ftpClient.disconnect();
return null;
}
//設置文件傳輸模式為二進制,可以保證傳輸的內容不會被改變
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
LOGGER.info("replyCode==========={}", replyCode);
} catch (IOException e) {
LOGGER.error("connect fail ------->>>{}", e.getMessage());
return null;
}
return ftpClient;