zhentao 4 days ago
parent
commit
1a6b0c49e3

+ 113 - 0
src/main/java/com/zhentao/osspicture/ossController.java

@@ -0,0 +1,113 @@
+package com.zhentao.osspicture;
+
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.model.PutObjectRequest;
+import com.zhentao.config.NullLogin;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.validation.constraints.Null;
+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")
+    @NullLogin
+    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();
+    }
+}

+ 0 - 2
src/main/java/com/zhentao/shouye/controller/UserShouyeController.java

@@ -17,11 +17,9 @@ public class UserShouyeController {
     @GetMapping("findAll")
     @NullLogin
     public Result findAll(@RequestHeader("token") String token){
-        System.err.println(token);
         String userIdFromToken = TokenUtils.getUserIdFromToken(token);
         UserShouyeDto userShouyeDto = new UserShouyeDto();
         userShouyeDto.setUid1(Long.valueOf(userIdFromToken));
-        System.err.println(userShouyeDto);
         return userShouyeService.findAll(userShouyeDto);
     }