修复FTP文件名或路径为中文无法预览的问题

This commit is contained in:
zhangyx 2025-05-16 11:22:54 +08:00
parent 6746325bf2
commit e604379a18

View File

@ -1,6 +1,7 @@
package cn.keking.utils;
import cn.keking.config.ConfigConstants;
import jodd.util.URLDecoder;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
@ -44,7 +45,13 @@ public class FtpUtils {
URL url = new URL(ftpUrl);
String host = url.getHost();
int port = (url.getPort() == -1) ? url.getDefaultPort() : url.getPort();
String remoteFilePath = url.getPath();
String path = url.getPath();
int fileNameStartIndex = path.lastIndexOf("/");
int fileNameLatestIndex = path.lastIndexOf(".");
String startPath = path.substring(0, fileNameStartIndex+1);
String endPath = path.substring(fileNameLatestIndex);
String fileName = path.substring(fileNameStartIndex+1, fileNameLatestIndex);
String remoteFilePath = startPath+ URLDecoder.decode(fileName, StringUtils.isEmpty(ftpControlEncoding) ? "UTF-8" : ftpControlEncoding) + endPath;
LOGGER.debug("FTP connection url:{}, username:{}, password:{}, controlEncoding:{}, localFilePath:{}", ftpUrl, username, password, controlEncoding, localFilePath);
FTPClient ftpClient = connect(host, port, username, password, controlEncoding);
OutputStream outputStream = Files.newOutputStream(Paths.get(localFilePath));