From 05600d089901d44e8d5036046025b6a90ceb896a Mon Sep 17 00:00:00 2001
From: heheng <heheng@123456>
Date: Tue, 03 Dec 2024 16:57:20 +0800
Subject: [PATCH] 修改及增加功能
---
expert-admin/src/main/java/com/gkhy/web/controller/common/CommonController.java | 65 +++++++++++++++++++++++---------
1 files changed, 47 insertions(+), 18 deletions(-)
diff --git a/expert-admin/src/main/java/com/gkhy/web/controller/common/CommonController.java b/expert-admin/src/main/java/com/gkhy/web/controller/common/CommonController.java
index d153648..c5c70eb 100644
--- a/expert-admin/src/main/java/com/gkhy/web/controller/common/CommonController.java
+++ b/expert-admin/src/main/java/com/gkhy/web/controller/common/CommonController.java
@@ -1,25 +1,29 @@
package com.gkhy.web.controller.common;
-import java.util.ArrayList;
-import java.util.List;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-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.multipart.MultipartFile;
-import com.gkhy.common.config.expertConfig;
+import com.gkhy.common.annotation.Anonymous;
+import com.gkhy.common.annotation.RepeatSubmit;
+import com.gkhy.common.config.ExpertConfig;
import com.gkhy.common.constant.Constants;
import com.gkhy.common.core.domain.AjaxResult;
+import com.gkhy.common.exception.ServiceException;
import com.gkhy.common.utils.StringUtils;
import com.gkhy.common.utils.file.FileUploadUtils;
import com.gkhy.common.utils.file.FileUtils;
import com.gkhy.framework.config.ServerConfig;
+import io.swagger.annotations.Api;
+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.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
/**
* 通用请求处理
@@ -28,6 +32,7 @@
*/
@RestController
@RequestMapping("/common")
+@Api(tags = "附件上传下载")
public class CommonController
{
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@@ -44,6 +49,7 @@
* @param delete 是否删除
*/
@GetMapping("/download")
+ @ApiOperation(value = "通用下载请求")
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
{
try
@@ -53,7 +59,7 @@
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
}
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
- String filePath = expertConfig.getDownloadPath() + fileName;
+ String filePath = ExpertConfig.getDownloadPath() + fileName;
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response, realFileName);
@@ -73,12 +79,15 @@
* 通用上传请求(单个)
*/
@PostMapping("/upload")
+ @ApiOperation(value = "通用上传请求(单个)")
+ @Anonymous
+ @RepeatSubmit
public AjaxResult uploadFile(MultipartFile file) throws Exception
{
try
{
// 上传文件路径
- String filePath = expertConfig.getUploadPath();
+ String filePath = ExpertConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileName;
@@ -99,12 +108,13 @@
* 通用上传请求(多个)
*/
@PostMapping("/uploads")
+ @ApiOperation(value = "通用上传请求(多个)")
public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
{
try
{
// 上传文件路径
- String filePath = expertConfig.getUploadPath();
+ String filePath = ExpertConfig.getUploadPath();
List<String> urls = new ArrayList<String>();
List<String> fileNames = new ArrayList<String>();
List<String> newFileNames = new ArrayList<String>();
@@ -136,6 +146,7 @@
* 本地资源通用下载
*/
@GetMapping("/download/resource")
+ @ApiOperation(value = "本地资源通用下载")
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
@@ -146,7 +157,7 @@
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
}
// 本地资源路径
- String localPath = expertConfig.getProfile();
+ String localPath = ExpertConfig.getProfile();
// 数据库资源地址
String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
// 下载名称
@@ -160,4 +171,22 @@
log.error("下载文件失败", e);
}
}
+
+
+ @ApiOperation(value = "删除附件")
+ @DeleteMapping("/removeFile")
+ public void removeFile(@RequestParam(required = true) String path){
+ // 本地资源路径
+ String localPath = ExpertConfig.getProfile();
+ // 数据库资源地址
+ String deletePath = localPath + path;
+ File dirFile=new File(deletePath);
+ if(!dirFile.exists()){
+ throw new ServiceException("文件不存在");
+ }
+ if(!dirFile.isFile()){
+ throw new ServiceException("非文件,不能删除");
+ }
+ dirFile.delete();
+ }
}
--
Gitblit v1.9.2