فهرست منبع

完善上传下载

chenzhengming 2 سال پیش
والد
کامیت
45fe7ba5a3

+ 27 - 22
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java

@@ -6,15 +6,13 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiOperation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import com.ruoyi.common.config.RuoYiConfig;
 import com.ruoyi.common.constant.Constants;
@@ -29,8 +27,9 @@ import com.ruoyi.framework.config.ServerConfig;
  * 
  * @author ruoyi
  */
+@Api(tags = "FileController",description = "文件通用上传下载以及多个文件上传")
 @RestController
-@RequestMapping("/common")
+@RequestMapping("/files")
 public class CommonController
 {
     private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@@ -46,8 +45,10 @@ public class CommonController
      * @param fileName 文件名称
      * @param delete 是否删除
      */
+    @ApiOperation(value = "文件下载", notes = "下载文件接口")
+    @ApiImplicitParam(name = "fileName", value = "文件地址", paramType = "query", required = true)
     @GetMapping("/download")
-    public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
+    public void fileDownload(@RequestParam String fileName, @RequestParam(required = false) Boolean delete, HttpServletResponse response, HttpServletRequest request)
     {
         try
         {
@@ -76,25 +77,27 @@ public class CommonController
      * 通用上传请求(单个)
      */
 
-    @PostMapping("/upload")
-    public AjaxResult uploadFile(MultipartFile file) throws Exception
+    @ApiOperation(value = "上传文件", notes = "上传文件接口")
+    @RequestMapping(value = "/send", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
+    public AjaxResult uploadFile(@RequestPart("fire")MultipartFile file) throws Exception
     {
-        try
-        {
+        try {
             // 上传文件路径
             String filePath = RuoYiConfig.getUploadPath();
-            // 上传并返回新文件名称
-            String fileName = FileUploadUtils.upload(filePath, file);
-            String url = serverConfig.getUrl() + fileName;
+            if (filePath == null){
+                throw new RuntimeException("找不到文件路径");
+            }
+            //上传并返回文件名
+            String filename = FileUploadUtils.upload(filePath, file);
+            if (filename == null){
+                throw new RuntimeException("获取不到文件名称");
+            }
+            String url = serverConfig.getUrl()+filename;
             AjaxResult ajax = AjaxResult.success();
-            ajax.put("url", url);
-            ajax.put("fileName", fileName);
-            ajax.put("newFileName", FileUtils.getName(fileName));
-            ajax.put("originalFilename", file.getOriginalFilename());
+            ajax.put("fileName",filename);
+            ajax.put("url",url);
             return ajax;
-        }
-        catch (Exception e)
-        {
+        }catch (Exception e){
             return AjaxResult.error(e.getMessage());
         }
     }
@@ -102,8 +105,9 @@ public class CommonController
     /**
      * 通用上传请求(多个)
      */
-    @PostMapping("/uploads")
-    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
+    @ApiOperation(value = "上传多个文件", notes = "上传多个文件接口")
+    @RequestMapping(value = "/sends", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
+    public AjaxResult uploadFiles(@RequestPart("fire")List<MultipartFile> files) throws Exception
     {
         try
         {
@@ -139,6 +143,7 @@ public class CommonController
     /**
      * 本地资源通用下载
      */
+
     @GetMapping("/download/resource")
     public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
             throws Exception

+ 0 - 51
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/FileController.java

@@ -1,51 +0,0 @@
-package com.ruoyi.web.controller.system;
-
-import com.ruoyi.common.config.RuoYiConfig;
-import com.ruoyi.common.core.domain.AjaxResult;
-import com.ruoyi.common.utils.file.FileUploadUtils;
-import com.ruoyi.framework.config.ServerConfig;
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiOperation;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
-
-@Api(tags = "FileController",description = "上传文件")
-@RestController
-@RequestMapping("/files")
-public class FileController {
-    @Autowired
-    private ServerConfig serverConfig;
-
-    /**
-     * 上传文件
-     */
-    /**
-     * 文件上传接口
-     */
-    @ApiOperation(value = "上传文件", notes = "上传文件接口")
-    @RequestMapping(value = "/send", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
-    public AjaxResult uploadFile(@RequestPart("fire")MultipartFile file) throws Exception
-    {
-        try {
-            // 上传文件路径
-            String filePath = RuoYiConfig.getUploadPath();
-            if (filePath == null){
-                throw new RuntimeException("找不到文件路径");
-            }
-            //上传并返回文件名
-            String filename = FileUploadUtils.upload(filePath, file);
-            if (filename == null){
-                throw new RuntimeException("获取不到文件名称");
-            }
-            String url = serverConfig.getUrl()+filename;
-            AjaxResult ajax = AjaxResult.success();
-            ajax.put("fileName",filename);
-            ajax.put("url",url);
-            return ajax;
-        }catch (Exception e){
-            return AjaxResult.error(e.getMessage());
-        }
-    }
-
-}