|
@@ -0,0 +1,49 @@
|
|
|
+package com.chelvc.cloud.maintain.controller;
|
|
|
+
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.Objects;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+import com.chelvc.framework.base.annotation.UnifiedResponseBody;
|
|
|
+import com.chelvc.framework.base.util.ErrorUtils;
|
|
|
+import com.chelvc.framework.upload.UploadHandler;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文件接口
|
|
|
+ *
|
|
|
+ * @author Woody
|
|
|
+ * @date 2023/5/4
|
|
|
+ */
|
|
|
+@Validated
|
|
|
+@RestController
|
|
|
+@UnifiedResponseBody
|
|
|
+@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
|
+public class FileController {
|
|
|
+ private final UploadHandler uploadHandler;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传图片
|
|
|
+ *
|
|
|
+ * @param file 图片文件
|
|
|
+ * @return 图片地址
|
|
|
+ * @throws IOException I/O异常
|
|
|
+ */
|
|
|
+ @PostMapping("/file/image")
|
|
|
+ public String uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
|
|
|
+ BufferedImage image;
|
|
|
+ try (InputStream input = file.getInputStream()) {
|
|
|
+ image = ImageIO.read(input);
|
|
|
+ }
|
|
|
+ ErrorUtils.availableResource(Objects.nonNull(image), "图片上传失败");
|
|
|
+ return this.uploadHandler.upload(image);
|
|
|
+ }
|
|
|
+}
|