From 82099528a7a5f500b4d8e9a1f10a11ecf468b9af Mon Sep 17 00:00:00 2001 From: Yangzhao Date: Sat, 4 Jul 2020 15:48:38 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=88=B0=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/diboot/file/util/HttpHelper.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/diboot-file-starter/src/main/java/com/diboot/file/util/HttpHelper.java b/diboot-file-starter/src/main/java/com/diboot/file/util/HttpHelper.java index ee07461..ec068d0 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/util/HttpHelper.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/util/HttpHelper.java @@ -248,16 +248,27 @@ public class HttpHelper { * @throws Exception */ public static void downloadLocalFile(String localFilePath, String exportFileName, HttpServletResponse response) throws Exception{ + downloadLocalFile(new File(localFilePath), exportFileName, response); + } + + /** + * 根据文件对象下载服务器文件 + * @param localFile 本地文件对象 + * @param exportFileName 导出文件的文件名 + * @param response + * @throws Exception + */ + public static void downloadLocalFile(File localFile, String exportFileName, HttpServletResponse response) throws Exception{ BufferedInputStream bis = null; BufferedOutputStream bos = null; try{ String fileName = new String(exportFileName.getBytes("utf-8"), "ISO8859-1"); - long fileLength = new File(localFilePath).length(); + long fileLength = localFile.length(); response.setContentType(getContextType(fileName)); response.setHeader("Content-disposition", "attachment; filename="+ fileName); response.setHeader("Content-Length", String.valueOf(fileLength)); response.setHeader("filename", URLEncoder.encode(exportFileName, StandardCharsets.UTF_8.name())); - bis = new BufferedInputStream(new FileInputStream(localFilePath)); + bis = new BufferedInputStream(new FileInputStream(localFile)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; @@ -266,7 +277,7 @@ public class HttpHelper { } } catch (Exception e) { - log.error("下载导出文件失败:"+localFilePath, e); + log.error("下载导出文件失败:"+localFile.getAbsolutePath(), e); } finally { if (bis != null) {