|
@@ -6,15 +6,13 @@ import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
|
|
+import io.swagger.annotations.ApiImplicitParam;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.MediaType;
|
|
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 org.springframework.web.multipart.MultipartFile;
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
import com.ruoyi.common.constant.Constants;
|
|
import com.ruoyi.common.constant.Constants;
|
|
@@ -29,8 +27,9 @@ import com.ruoyi.framework.config.ServerConfig;
|
|
*
|
|
*
|
|
* @author ruoyi
|
|
* @author ruoyi
|
|
*/
|
|
*/
|
|
|
|
+@Api(tags = "FileController",description = "文件通用上传下载以及多个文件上传")
|
|
@RestController
|
|
@RestController
|
|
-@RequestMapping("/common")
|
|
|
|
|
|
+@RequestMapping("/files")
|
|
public class CommonController
|
|
public class CommonController
|
|
{
|
|
{
|
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
@@ -46,8 +45,10 @@ public class CommonController
|
|
* @param fileName 文件名称
|
|
* @param fileName 文件名称
|
|
* @param delete 是否删除
|
|
* @param delete 是否删除
|
|
*/
|
|
*/
|
|
|
|
+ @ApiOperation(value = "文件下载", notes = "下载文件接口")
|
|
|
|
+ @ApiImplicitParam(name = "fileName", value = "文件地址", paramType = "query", required = true)
|
|
@GetMapping("/download")
|
|
@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
|
|
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 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();
|
|
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;
|
|
return ajax;
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
|
|
+ }catch (Exception e){
|
|
return AjaxResult.error(e.getMessage());
|
|
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
|
|
try
|
|
{
|
|
{
|
|
@@ -139,6 +143,7 @@ public class CommonController
|
|
/**
|
|
/**
|
|
* 本地资源通用下载
|
|
* 本地资源通用下载
|
|
*/
|
|
*/
|
|
|
|
+
|
|
@GetMapping("/download/resource")
|
|
@GetMapping("/download/resource")
|
|
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
|
|
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
|
|
throws Exception
|
|
throws Exception
|