修复本地服务上传中文名下载不了的问题

This commit is contained in:
weiweiw 2024-09-26 13:00:45 +08:00
parent a10cd6db85
commit 28103703fe
1 changed files with 19 additions and 7 deletions

View File

@ -39,14 +39,20 @@ public class FileDownloadUtils {
}
private static boolean downloadHttpFile(HttpServletResponse response, String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//处理中文文件名的问题
String fileName = urlStr.substring(urlStr.lastIndexOf('/') + 1);
String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
String newPath = urlStr.substring(0, urlStr.lastIndexOf('/') + 1) + encodedFileName;
URL urlNew = new URL(newPath);
HttpURLConnection connection = (HttpURLConnection) urlNew.openConnection();
connection.setRequestMethod("GET");
com.bonus.file.utils.FileUtils.setResponseHeaderByUrl(response, urlStr);
try (InputStream inputStream = connection.getInputStream();
ServletOutputStream outputStream = response.getOutputStream()) {
com.bonus.file.utils.FileUtils.setResponseHeaderByUrl(response, urlStr);
try {
int code = connection.getResponseCode();
InputStream inputStream = connection.getInputStream();
ServletOutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
@ -74,7 +80,13 @@ public class FileDownloadUtils {
String[] credentials = userInfo != null ? userInfo.split(":") : new String[]{"anonymous", ""};
String username = credentials[0];
String password = credentials.length > 1 ? credentials[1] : "";
String remoteFilePath = url.getPath();
//处理中文文件名的问题
String urlPath = url.getPath();
String path = urlPath.substring(urlStr.lastIndexOf('/') + 1);
String encodedFileName = URLEncoder.encode(path, "UTF-8");
String remoteFilePath = urlPath.substring(0, urlStr.lastIndexOf('/') + 1) + encodedFileName;;
ftpClient.connect(host, port);
ftpClient.login(username, password);