解决下载文件乱码的问题

This commit is contained in:
chenxudong 2024-11-28 09:48:31 +08:00
parent df92c856af
commit 8344baed68
1 changed files with 5 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package com.electromagnetic.industry.software.data.manage.service.facade;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert;
@ -186,28 +187,20 @@ public class EDDataFacadeImpl implements EDDataFacade {
String filePath = storageFilePath + "/" + filePathOfFolder + "/" + edDataInfo.getDataName();
Assert.isTrue(FileUtil.exist(filePath), "下载文件不存在。");
File file = new File(filePath);
FileSystemResource fileSystemResource = new FileSystemResource(file);
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
String fileName = fileSystemResource.getFilename();
byte[] fileNameBytes = fileName.getBytes(StandardCharsets.UTF_8);
fileName = new String(fileNameBytes, 0, fileNameBytes.length, StandardCharsets.ISO_8859_1);
String attachment = StrFormatter.format("attachment;filename={}", fileName);
//headers.add("Content-Disposition", attachment);
// headers.set("attachment", fileName);
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
// response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileSystemResource.getFilename().getBytes("UTF-8"), "ISO8859-1"));
String fileName = Base64.encode(fileSystemResource.getFilename());
response.setHeader("content-disposition","attachment;filename=" + fileName);
// 构建响应实体(可以返回<byte[]或Resource返回类型取决body入参类型)
return ResponseEntity
.ok()
.headers(headers)
.contentLength(fileSystemResource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.contentType(MediaType.parseMediaType("application/octet-stream;charset=UTF-8"))
.body(new InputStreamResource(fileSystemResource.getInputStream()));
}
/**