From c6d2dc78ac9d0db175ea005a7e9be63f9873c788 Mon Sep 17 00:00:00 2001 From: jlzhou <12020042@qq.com> Date: Tue, 12 Nov 2024 10:39:39 +0800 Subject: [PATCH] upload UploadController --- .../web/controller/UploadController.java | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/UploadController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/UploadController.java index 8e48f83..b33ba2e 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/UploadController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/UploadController.java @@ -1,6 +1,8 @@ package com.ruoyi.web.controller; +import cn.dev33.satoken.annotation.SaIgnore; import cn.hutool.core.util.ObjectUtil; +import com.ruoyi.common.annotation.Dev; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.enums.BusinessType; @@ -12,38 +14,41 @@ import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + @RequiredArgsConstructor @RestController @RequestMapping("/") public class UploadController { - private final FileService fileService; + private final FileService fileService; - @GetMapping("/uploadKey") - public R uploadKey() { - return R.ok().setData(FileUtils.getUploadKey()); - } + @GetMapping("/uploadKey") + public R uploadKey(){ + return R.ok().setData(FileUtils.getUploadKey()); + } - @Log(title = "OSS对象存储", businessType = BusinessType.INSERT) - @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public R upload(@RequestPart("file") MultipartFile file, String pre) { - if (ObjectUtil.isNull(file)) { - throw new ServiceException("文件为空"); + @Log(title = "OSS对象存储", businessType = BusinessType.INSERT) + @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public R upload(@RequestPart("file") MultipartFile file, String pre) { + if (ObjectUtil.isNull(file)) { + throw new ServiceException("文件为空"); + } + return R.map().put("ossId",0).put("url",fileService.setPrefix(pre).setKeepFilename().save(file)); } - return R.map().put("ossId", 0).put("url", fileService.setPrefix(pre).setKeepFilename().save(file)); - } - - @Log(title = "OSS对象存储", businessType = BusinessType.INSERT) - @PostMapping(value = "/uploadImg", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public R uploadImg(@RequestPart("file") MultipartFile file, String pre) { - if (ObjectUtil.isNull(file)) { - throw new ServiceException("文件为空"); - } - if (!file.getContentType().startsWith("image/")) { - throw new ServiceException("不是图片"); + + @Log(title = "OSS对象存储", businessType = BusinessType.INSERT) + @PostMapping(value = "/uploadImg", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public R uploadImg(@RequestPart("file") MultipartFile file, String pre) { + if (ObjectUtil.isNull(file)) { + throw new ServiceException("文件为空"); + } + if(!file.getContentType().startsWith("image/")){ + throw new ServiceException("不是图片"); + } + return R.map().put("ossId",0).put("url",fileService.setPrefix(pre).setSize().setWatermark().setThumbnail().saveImage(file)); } - return R.map().put("ossId", 0).put("url", fileService.setPrefix(pre).setSize().setWatermark().setThumbnail().saveImage(file)); - } }