From 28103703fe742669563b2ed8d92f77f0d3c465bf Mon Sep 17 00:00:00 2001 From: weiweiw <14335254+weiweiw22@user.noreply.gitee.com> Date: Thu, 26 Sep 2024 13:00:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=AC=E5=9C=B0=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E4=B8=8A=E4=BC=A0=E4=B8=AD=E6=96=87=E5=90=8D=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E4=B8=8D=E4=BA=86=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bonus/file/utils/FileDownloadUtils.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) 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);