fix: FTP上传中文乱码

This commit is contained in:
裴浩宇 2024-05-08 16:56:53 +08:00
parent 2c471fd076
commit b86961c9d4

View File

@ -76,7 +76,13 @@ public class FtpTool {
ftpClient.setConnectTimeout(TIMEOUT); ftpClient.setConnectTimeout(TIMEOUT);
ftpClient.connect(HOST, PORT); ftpClient.connect(HOST, PORT);
ftpClient.login(USER_NAME, PWD); ftpClient.login(USER_NAME, PWD);
ftpClient.setControlEncoding("UTF-8"); //开启服务器对UTF-8的支持如果服务器支持就用UTF-8编码否则就用本地编码ISO-8859-1
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {
ftpClient.setControlEncoding("UTF-8");
} else {
//FTP协议里面规定文件名编码为iso-8859-1
ftpClient.setControlEncoding("ISO-8859-1");
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.error("连接FTP失败用户名或密码错误用户名{};密码{}", USER_NAME, PWD); log.error("连接FTP失败用户名或密码错误用户名{};密码{}", USER_NAME, PWD);