this is 4.2 update
This commit is contained in:
parent
82aef63706
commit
3711d70e5b
|
@ -102,7 +102,7 @@ public class FileController {
|
|||
@Operation(summary = "Web端文件上传(服务器)", description = "参数:文件对象(multipartFile), 业务类型(biz),权限:管理员,方法名:uploadFile")
|
||||
public BaseResponse<String> uploadFileToServe(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) throws IOException {
|
||||
// 校验用户是否登录
|
||||
userService.getLoginUser(request);
|
||||
// userService.getLoginUser(request);
|
||||
// 校验文件
|
||||
validFile(multipartFile);
|
||||
// 获取文件的保存路径
|
||||
|
@ -142,12 +142,31 @@ public class FileController {
|
|||
filePath = UPLOAD_DIR + "feiyi/img/" + filename;
|
||||
}
|
||||
File file = new File(filePath);
|
||||
// 清空response
|
||||
response.reset();
|
||||
// 设置response的Header
|
||||
// 检查文件是否存在
|
||||
if (!file.exists()) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
|
||||
// // 设置response的Header
|
||||
response.setContentType("application/octet-stream");
|
||||
response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(file.getName(), StandardCharsets.UTF_8));
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentLengthLong(file.length()); // 使用 setContentLengthLong 适应大文件
|
||||
|
||||
// 设置缓存相关的 HTTP 头
|
||||
long lastModified = file.lastModified();
|
||||
response.setDateHeader("Last-Modified", lastModified); // 文件最后修改时间
|
||||
response.setHeader("Cache-Control", "public, max-age=86400"); // 缓存一天(24小时)
|
||||
response.setHeader("ETag", String.valueOf(lastModified)); // 使用文件最后修改时间作为 ETag
|
||||
|
||||
// 检查浏览器缓存是否有效
|
||||
String ifNoneMatch = response.getHeader("If-None-Match");
|
||||
if (ifNoneMatch != null && ifNoneMatch.equals(String.valueOf(lastModified))) {
|
||||
// 如果 ETag 匹配,表示文件没有变化,返回 304 Not Modified
|
||||
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用 BufferedInputStream 和 BufferedOutputStream 提高性能
|
||||
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
|
||||
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream(), BUFFER_SIZE)) {
|
||||
|
@ -176,11 +195,16 @@ public class FileController {
|
|||
filename = "feiyi/fonts/" + filename; // 字体路径
|
||||
String filePath = UPLOAD_DIR + filename;
|
||||
File file = new File(filePath);
|
||||
// // 清空response
|
||||
// response.reset();
|
||||
// 设置response的Header
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!file.exists()) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
|
||||
// // 设置response的Header
|
||||
response.setContentType("application/octet-stream");
|
||||
response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
|
||||
response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(file.getName(), StandardCharsets.UTF_8));
|
||||
response.setContentLengthLong(file.length()); // 使用 setContentLengthLong 适应大文件
|
||||
|
||||
// 设置缓存相关的 HTTP 头
|
||||
|
@ -189,8 +213,13 @@ public class FileController {
|
|||
response.setHeader("Cache-Control", "public, max-age=86400"); // 缓存一天(24小时)
|
||||
response.setHeader("ETag", String.valueOf(lastModified)); // 使用文件最后修改时间作为 ETag
|
||||
|
||||
|
||||
|
||||
// 检查浏览器缓存是否有效
|
||||
String ifNoneMatch = response.getHeader("If-None-Match");
|
||||
if (ifNoneMatch != null && ifNoneMatch.equals(String.valueOf(lastModified))) {
|
||||
// 如果 ETag 匹配,表示文件没有变化,返回 304 Not Modified
|
||||
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
||||
return;
|
||||
}
|
||||
|
||||
// 使用 BufferedInputStream 和 BufferedOutputStream 提高性能
|
||||
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
|
||||
|
|
Loading…
Reference in New Issue
Block a user