fix: FileController恢复标准实现,确保编译和部署正常

This commit is contained in:
MaDaLei 2026-04-17 20:31:50 +08:00
parent fd6bf7c071
commit 6cbf23c06a

View File

@ -3,13 +3,18 @@ package com.petstore.controller;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Locale;
@ -23,14 +28,10 @@ import java.util.UUID;
@CrossOrigin
public class FileController {
private static final Set<String> IMAGE_EXT = Set.of(
".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".heic", ".heif", ".jpe"
);
private static final Set<String> VIDEO_EXT = Set.of(
".mp4", ".mov", ".m4v", ".webm", ".avi", ".mkv", ".3gp"
);
private final Set<String> IMAGE_EXT = Set.of(".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".heic", ".heif");
private final Set<String> VIDEO_EXT = Set.of(".mp4", ".mov", ".avi", ".mkv", ".flv", ".wmv", ".webm");
@Value("${upload.path:uploads/}")
@Value("${upload.path}")
private String uploadPath;
/**
@ -61,55 +62,43 @@ public class FileController {
}
@GetMapping("/image/**")
public void getImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
public ResponseEntity<Resource> getImage(HttpServletRequest request) throws IOException {
String path = request.getRequestURI().replace("/api/upload/image", "");
String basePath = uploadPath.endsWith("/") ? uploadPath : uploadPath + "/";
File file = new File(basePath + path);
if (!file.exists()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
return ResponseEntity.notFound().build();
}
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) contentType = "image/jpeg";
// 去掉可能的 charset 参数
// 去掉 charset 参数Spring Accept-Charset 会错误地给 binary 类型加上 charset
int semi = contentType.indexOf(';');
if (semi >= 0) contentType = contentType.substring(0, semi).trim();
// 手动设置响应头彻底掌控 Content-Type绕过 Spring ResourceHttpMessageConverter
response.setContentType(contentType);
response.setContentLengthLong(file.length());
response.setHeader("Accept-Ranges", "bytes");
// 支持范围请求HTTP 206video 播放器需要
response.setHeader("Content-Disposition", "inline");
try (var in = Files.newInputStream(file.toPath());
var out = response.getOutputStream()) {
in.transferTo(out);
out.flush();
}
MediaType mediaType = MediaType.parseMediaType(contentType);
return ResponseEntity.ok()
.contentType(mediaType)
.contentLength(file.length())
.body(new FileSystemResource(file));
}
// 兼容旧路径/2026/04/01/xxx.jpg
@GetMapping("/legacy/**")
public void getLegacyImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
public ResponseEntity<Resource> getLegacyImage(HttpServletRequest request) throws IOException {
String path = request.getRequestURI().replace("/api/upload/legacy", "");
String basePath = uploadPath.endsWith("/") ? uploadPath : uploadPath + "/";
File file = new File(basePath + path);
if (!file.exists()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
return ResponseEntity.notFound().build();
}
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) contentType = "image/jpeg";
int semi = contentType.indexOf(';');
if (semi >= 0) contentType = contentType.substring(0, semi).trim();
response.setContentType(contentType);
response.setContentLengthLong(file.length());
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-Disposition", "inline");
try (var in = Files.newInputStream(file.toPath());
var out = response.getOutputStream()) {
in.transferTo(out);
out.flush();
}
MediaType mediaType = MediaType.parseMediaType(contentType);
return ResponseEntity.ok()
.contentType(mediaType)
.contentLength(file.length())
.body(new FileSystemResource(file));
}
/** produces 显式 UTF-8避免网关/客户端按 ISO-8859-1 解码导致 message 中文乱码 */
@ -155,6 +144,7 @@ public class FileController {
}
ext = looksVideo ? ".mp4" : ".jpg";
}
String filename = UUID.randomUUID().toString().replace("-", "") + ext;
// 保存文件流式写入避免大视频一次性进内存