fix: 企标部分文件下载 响应头编码报错

响应头中非ASCII字符需要编码后传入
This commit is contained in:
2024-02-21 10:50:37 +08:00
parent 40e95fbc71
commit e0a2b95f27
4 changed files with 36 additions and 7 deletions
@@ -28,6 +28,8 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
@@ -155,7 +157,11 @@ public class ESAnnualInitController {
try {
ClassPathResource resource = new ClassPathResource("excelTemplate/年度制修订计划(标准化发起)导入模板.xls");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=年度制修订计划(标准化发起)导入模板.xls");
response.setCharacterEncoding("UTF-8"); // 这行确保响应流使用UTF-8编码
// 处理文件名乱码问题
String encodedFileName = URLEncoder.encode("年度制修订计划(标准化发起)导入模板", StandardCharsets.UTF_8.name()).replace("+", "%20");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName + ".xls");
try (InputStream in = resource.getInputStream(); OutputStream out = response.getOutputStream()) {
IOUtils.copy(in, out);
@@ -34,6 +34,8 @@ import java.io.OutputStream;
import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
@@ -67,7 +69,11 @@ public class ESAnnualReportController {
try {
ClassPathResource resource = new ClassPathResource("excelTemplate/年度制修订计划(各部门主动上报)导入模板.xls");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=年度制修订计划(各部门主动上报)导入模板.xls");
response.setCharacterEncoding("UTF-8"); // 这行确保响应流使用UTF-8编码
// 处理文件名乱码问题
String encodedFileName = URLEncoder.encode("年度制修订计划(各部门主动上报)导入模板", StandardCharsets.UTF_8.name()).replace("+", "%20");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName + ".xls");
try (InputStream in = resource.getInputStream(); OutputStream out = response.getOutputStream()) {
IOUtils.copy(in, out);