fix: 企标部分文件下载 响应头编码报错
响应头中非ASCII字符需要编码后传入
This commit is contained in:
+7
-1
@@ -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);
|
||||
|
||||
+7
-1
@@ -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);
|
||||
|
||||
+14
-3
@@ -62,6 +62,7 @@ import com.jero.modules.tag.enums.FieldShowTypeEnum;
|
||||
import com.jero.modules.tag.enums.LawsFieldTypeEnum;
|
||||
import com.jero.modules.tag.enums.TableNameEnum;
|
||||
import com.jero.modules.tag.service.ILawsAreaService;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
@@ -82,6 +83,7 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -1525,6 +1527,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
* @param tableName
|
||||
*/
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void templateDownload(HttpServletResponse response, String tableName) {
|
||||
// 获取全部字段
|
||||
List<LawsTag> fieldList = lawsCommonMapper.getFieldList(tableName);
|
||||
@@ -1550,7 +1553,11 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
|
||||
// 设置HTTP响应头
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"导入模板.xlsx\"");
|
||||
response.setCharacterEncoding("UTF-8"); // 这行确保响应流使用UTF-8编码
|
||||
|
||||
// 处理文件名乱码问题
|
||||
String encodedFileName = URLEncoder.encode("导入模板", StandardCharsets.UTF_8.name()).replace("+", "%20");
|
||||
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName + ".xlsx");
|
||||
|
||||
// 获取响应输出流并写入Excel
|
||||
try (OutputStream out = response.getOutputStream()) {
|
||||
@@ -2441,8 +2448,12 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
|
||||
}
|
||||
|
||||
// 4. 设置HTTP响应头,并返回ZIP文件
|
||||
response.setContentType("application/zip");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"导入模板.zip\"");
|
||||
response.setContentType("application/zip;charset=UTF-8");
|
||||
response.setCharacterEncoding("UTF-8"); // 这行确保响应流使用UTF-8编码
|
||||
|
||||
// 处理文件名乱码问题
|
||||
String encodedFileName = URLEncoder.encode("导入模板", StandardCharsets.UTF_8.name()).replace("+", "%20");
|
||||
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName + ".zip");
|
||||
|
||||
// 5. 输出ZIP文件到响应流中
|
||||
try (OutputStream out = response.getOutputStream()) {
|
||||
|
||||
+8
-2
@@ -27,6 +27,8 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -112,8 +114,12 @@ public class EnterpriseStandardPlanServiceImpl extends ServiceImpl<EnterpriseSta
|
||||
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EnterpriseStandardPlanVo.class, records);
|
||||
|
||||
// 设置响应头和内容类型
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
|
||||
response.setContentType("application/vnd.ms-excel;charset=UTF-8"); // 显式设置字符编码
|
||||
response.setCharacterEncoding("UTF-8"); // 这行确保响应流使用UTF-8编码
|
||||
|
||||
// 处理文件名乱码问题
|
||||
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()).replace("+", "%20");
|
||||
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName + ".xls");
|
||||
|
||||
// 写出到响应流
|
||||
workbook.write(response.getOutputStream());
|
||||
|
||||
Reference in New Issue
Block a user