|
@@ -1,4 +1,107 @@
|
|
|
package com.zhentao.oss.controller;
|
|
|
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
+import com.aliyun.oss.model.PutObjectRequest;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/upload")
|
|
|
public class ossController {
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.endpoint}")
|
|
|
+ private String endpoint;
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.accessKeyId}")
|
|
|
+ private String accessKeyId;
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.accessKeySecret}")
|
|
|
+ private String accessKeySecret;
|
|
|
+
|
|
|
+ @Value("${aliyun.oss.bucketName}")
|
|
|
+ private String bucketName;
|
|
|
+
|
|
|
+ // 允许的图片格式
|
|
|
+ private static final String[] ALLOWED_TYPES = {
|
|
|
+ "image/jpeg", "image/png", "image/gif"
|
|
|
+ };
|
|
|
+
|
|
|
+ @PostMapping("/simple-image")
|
|
|
+ public Map<String, Object> uploadSimpleImage(@RequestParam("file") MultipartFile file) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ // 基础验证
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
+ result.put("code", 400);
|
|
|
+ result.put("message", "上传图片不能为空");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证图片格式
|
|
|
+ String contentType = file.getContentType();
|
|
|
+ if (!isValidImageType(contentType)) {
|
|
|
+ result.put("code", 400);
|
|
|
+ result.put("message", "仅支持JPG、PNG、GIF格式的图片");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ OSS ossClient = null;
|
|
|
+ try {
|
|
|
+ ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+
|
|
|
+ // 生成唯一文件名
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String extension = getFileExtension(originalFilename);
|
|
|
+ String fileName = UUID.randomUUID().toString() + "." + extension;
|
|
|
+
|
|
|
+ // 上传图片
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(
|
|
|
+ bucketName, fileName, file.getInputStream()
|
|
|
+ );
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
+
|
|
|
+ // 构建访问URL
|
|
|
+ String endpointWithoutProtocol = endpoint.replaceFirst("^https?://", "");
|
|
|
+ String imageUrl = "https://" + bucketName + "." + endpointWithoutProtocol + "/" + fileName;
|
|
|
+
|
|
|
+ // 返回结果
|
|
|
+ result.put("code", 200);
|
|
|
+ result.put("message", "图片上传成功");
|
|
|
+ result.put("imageUrl", imageUrl);
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ result.put("code", 500);
|
|
|
+ result.put("message", "上传失败:" + e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isValidImageType(String contentType) {
|
|
|
+ if (contentType == null) return false;
|
|
|
+ for (String type : ALLOWED_TYPES) {
|
|
|
+ if (contentType.equalsIgnoreCase(type)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFileExtension(String fileName) {
|
|
|
+ if (fileName == null || !fileName.contains(".")) {
|
|
|
+ return "jpg";
|
|
|
+ }
|
|
|
+ return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ }
|
|
|
}
|