diff --git a/bonus-modules/bonus-file/src/main/java/com/bonus/file/utils/FileDownloadUtils.java b/bonus-modules/bonus-file/src/main/java/com/bonus/file/utils/FileDownloadUtils.java index 7024428..5a7ea9c 100644 --- a/bonus-modules/bonus-file/src/main/java/com/bonus/file/utils/FileDownloadUtils.java +++ b/bonus-modules/bonus-file/src/main/java/com/bonus/file/utils/FileDownloadUtils.java @@ -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);