1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
| import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
@RestController
public class ImageUploadController {
private static final String UPLOAD_DIR = "uploads";
private static final String[] ALLOWED_TYPES = {"image/jpeg", "image/png", "image/gif"};
private static final int MAX_WIDTH = 1920;
private static final int MAX_HEIGHT = 1080;
private static final int TARGET_WIDTH = 800;
private static final int TARGET_HEIGHT = 600;
@PostMapping("/upload")
public ResponseEntity<String> uploadImage(@RequestParam("image") MultipartFile file) {
if (file.isEmpty()) {
return new ResponseEntity<>("请选择要上传的图片", HttpStatus.BAD_REQUEST);
}
// 检查文件类型
String contentType = file.getContentType();
boolean isAllowed = false;
for (String allowedType : ALLOWED_TYPES) {
if (allowedType.equals(contentType)) {
isAllowed = true;
break;
}
}
if (!isAllowed) {
return new ResponseEntity<>("不支持的文件类型,仅支持 JPEG、PNG 和 GIF 格式", HttpStatus.BAD_REQUEST);
}
try {
BufferedImage image = ImageIO.read(file.getInputStream());
int width = image.getWidth();
int height = image.getHeight();
// 检查图片尺寸
if (width > MAX_WIDTH || height > MAX_HEIGHT) {
return new ResponseEntity<>("图片尺寸过大,最大宽度为 " + MAX_WIDTH + ",最大高度为 " + MAX_HEIGHT, HttpStatus.BAD_REQUEST);
}
// 裁剪和压缩图片
BufferedImage resizedImage = resizeImage(image, TARGET_WIDTH, TARGET_HEIGHT);
// 创建上传目录(如果不存在)
File uploadDir = new File(UPLOAD_DIR);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
// 获取文件名
String fileName = file.getOriginalFilename();
Path filePath = Paths.get(UPLOAD_DIR, fileName);
// 保存处理后的图片
String format = contentType.split("/")[1];
ImageIO.write(resizedImage, format, filePath.toFile());
return new ResponseEntity<>("图片上传并处理成功", HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity<>("图片上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
private BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = resizedImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
graphics2D.dispose();
return resizedImage;
}
}
|