From 1b461cf7a8dced14fe725e0733a5d67678b63197 Mon Sep 17 00:00:00 2001 From: duhuafei Date: Tue, 25 Jun 2019 13:31:10 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8RFC=205987=E6=A0=87=E5=87=86=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E4=B9=B1=E7=A0=81=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E5=8E=BBUA=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/excel/poi/ExcelBoot.java | 9 ++--- .../excel/poi/common/WebFilenameUtils.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/excel/poi/common/WebFilenameUtils.java diff --git a/src/main/java/com/excel/poi/ExcelBoot.java b/src/main/java/com/excel/poi/ExcelBoot.java index c29f43b..ad2ad21 100644 --- a/src/main/java/com/excel/poi/ExcelBoot.java +++ b/src/main/java/com/excel/poi/ExcelBoot.java @@ -17,6 +17,7 @@ package com.excel.poi; import com.excel.poi.common.Constant; +import com.excel.poi.common.WebFilenameUtils; import com.excel.poi.entity.ExcelEntity; import com.excel.poi.excel.ExcelReader; import com.excel.poi.excel.ExcelWriter; @@ -193,7 +194,7 @@ public void exportResponse(R param, ExportFunction exportFunction) try { verifyResponse(); sxssfWorkbook = commonSingleSheet(param, exportFunction); - download(sxssfWorkbook, httpServletResponse, URLEncoder.encode(fileName + ".xlsx", "UTF-8")); + download(sxssfWorkbook, httpServletResponse, fileName + ".xlsx"); } finally { if (sxssfWorkbook != null) { sxssfWorkbook.close(); @@ -273,7 +274,7 @@ public void exportMultiSheetResponse(R param, ExportFunction export try { verifyResponse(); sxssfWorkbook = commonMultiSheet(param, exportFunction); - download(sxssfWorkbook, httpServletResponse, URLEncoder.encode(fileName + ".xlsx", "UTF-8")); + download(sxssfWorkbook, httpServletResponse, fileName + ".xlsx"); } finally { if (sxssfWorkbook != null) { sxssfWorkbook.close(); @@ -352,7 +353,7 @@ public void exportTemplate() { ExcelEntity excelMapping = ExcelMappingFactory.loadExportExcelClass(excelClass, fileName); ExcelWriter excelWriter = new ExcelWriter(excelMapping, pageSize, rowAccessWindowSize, recordCountPerSheet, openAutoColumWidth); sxssfWorkbook = excelWriter.generateTemplateWorkbook(); - download(sxssfWorkbook, httpServletResponse, URLEncoder.encode(fileName + ".xlsx", "UTF-8")); + download(sxssfWorkbook, httpServletResponse, fileName + ".xlsx"); } finally { if (sxssfWorkbook != null) { sxssfWorkbook.close(); @@ -432,7 +433,7 @@ private void download(SXSSFWorkbook wb, HttpServletResponse response, String fil OutputStream out = response.getOutputStream(); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", - String.format("attachment; filename=%s", filename)); + WebFilenameUtils.disposition(filename)); if (null != out) { wb.write(out); out.flush(); diff --git a/src/main/java/com/excel/poi/common/WebFilenameUtils.java b/src/main/java/com/excel/poi/common/WebFilenameUtils.java new file mode 100644 index 0000000..9e51ae7 --- /dev/null +++ b/src/main/java/com/excel/poi/common/WebFilenameUtils.java @@ -0,0 +1,36 @@ +package com.excel.poi.common; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.UnsupportedEncodingException; + +/**根据RFC 5987规范生成disposition值, 解决浏览器兼容以及中文乱码问题 + * @author BiLuohen + * @date 6/25/2019 + */ +public class WebFilenameUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(WebFilenameUtils.class); + + private static final String DISPOSITION_FORMAT = "attachment; filename=\"%s\"; filename*=utf-8''%s"; + + /** + * 未编码文件名转Content-Disposition值 + * + * @param filename 未编码的文件名(包含文件后缀) + * @return Content-Disposition值 + */ + public static String disposition(String filename) { + String codedFilename = filename; + try { + if (!StringUtil.isBlank(filename)) { + codedFilename = java.net.URLEncoder.encode(filename, "UTF-8"); + } + } catch (UnsupportedEncodingException e) { + LOGGER.error("不支持的编码:", e); + } + return String.format(DISPOSITION_FORMAT, codedFilename, codedFilename); + + } +}