|
|
@@ -1,11 +1,14 @@
|
|
|
package com.ktg.web.controller.common;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
import com.ktg.common.config.RuoYiConfig;
|
|
|
+import com.ktg.common.constant.Constants;
|
|
|
+import com.ktg.common.core.domain.AjaxResult;
|
|
|
+import com.ktg.common.utils.StringUtils;
|
|
|
+import com.ktg.common.utils.file.FileUploadUtils;
|
|
|
+import com.ktg.common.utils.file.FileUtils;
|
|
|
+import com.ktg.framework.config.ServerConfig;
|
|
|
+import com.ktg.system.domain.SysUploadFile;
|
|
|
+import com.ktg.system.service.ISysUploadFileService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -15,42 +18,39 @@ 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.ktg.common.constant.Constants;
|
|
|
-import com.ktg.common.core.domain.AjaxResult;
|
|
|
-import com.ktg.common.utils.StringUtils;
|
|
|
-import com.ktg.common.utils.file.FileUploadUtils;
|
|
|
-import com.ktg.common.utils.file.FileUtils;
|
|
|
-import com.ktg.framework.config.ServerConfig;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 通用请求处理
|
|
|
- *
|
|
|
+ *
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping("/common")
|
|
|
-public class CommonController
|
|
|
-{
|
|
|
+public class CommonController {
|
|
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
|
|
|
|
@Autowired
|
|
|
private ServerConfig serverConfig;
|
|
|
+ @Autowired
|
|
|
+ private ISysUploadFileService iSysUploadFileService;
|
|
|
|
|
|
private static final String FILE_DELIMETER = ",";
|
|
|
|
|
|
/**
|
|
|
* 通用下载请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param fileName 文件名称
|
|
|
- * @param delete 是否删除
|
|
|
+ * @param delete 是否删除
|
|
|
*/
|
|
|
@GetMapping("/download")
|
|
|
- public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- if (!FileUtils.checkAllowDownload(fileName))
|
|
|
- {
|
|
|
+ public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ if (!FileUtils.checkAllowDownload(fileName)) {
|
|
|
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
|
|
|
}
|
|
|
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
|
|
|
@@ -59,13 +59,10 @@ public class CommonController
|
|
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
FileUtils.setAttachmentResponseHeader(response, realFileName);
|
|
|
FileUtils.writeBytes(filePath, response.getOutputStream());
|
|
|
- if (delete)
|
|
|
- {
|
|
|
+ if (delete) {
|
|
|
FileUtils.deleteFile(filePath);
|
|
|
}
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
log.error("下载文件失败", e);
|
|
|
}
|
|
|
}
|
|
|
@@ -74,10 +71,8 @@ public class CommonController
|
|
|
* 通用上传请求(单个)
|
|
|
*/
|
|
|
@PostMapping("/upload")
|
|
|
- public AjaxResult uploadFile(MultipartFile file) throws Exception
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public AjaxResult uploadFile(MultipartFile file) throws Exception {
|
|
|
+ try {
|
|
|
// 上传文件路径
|
|
|
String filePath = RuoYiConfig.getUploadPath();
|
|
|
// 上传并返回新文件名称
|
|
|
@@ -88,10 +83,17 @@ public class CommonController
|
|
|
ajax.put("fileName", fileName);
|
|
|
ajax.put("newFileName", FileUtils.getName(fileName));
|
|
|
ajax.put("originalFilename", file.getOriginalFilename());
|
|
|
+
|
|
|
+ // 2.开始新增文件上传信息
|
|
|
+ SysUploadFile sysFile = new SysUploadFile();
|
|
|
+ sysFile.setName(fileName);
|
|
|
+ sysFile.setPath(filePath);
|
|
|
+ sysFile.setUrl(url);
|
|
|
+ sysFile.setType(file.getContentType());
|
|
|
+ sysFile.setSize(file.getSize());
|
|
|
+ iSysUploadFileService.insertSysUploadFile(sysFile);
|
|
|
return ajax;
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return AjaxResult.error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
@@ -100,18 +102,15 @@ public class CommonController
|
|
|
* 通用上传请求(多个)
|
|
|
*/
|
|
|
@PostMapping("/uploads")
|
|
|
- public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
|
|
|
+ try {
|
|
|
// 上传文件路径
|
|
|
String filePath = RuoYiConfig.getUploadPath();
|
|
|
List<String> urls = new ArrayList<String>();
|
|
|
List<String> fileNames = new ArrayList<String>();
|
|
|
List<String> newFileNames = new ArrayList<String>();
|
|
|
List<String> originalFilenames = new ArrayList<String>();
|
|
|
- for (MultipartFile file : files)
|
|
|
- {
|
|
|
+ for (MultipartFile file : files) {
|
|
|
// 上传并返回新文件名称
|
|
|
String fileName = FileUploadUtils.upload(filePath, file);
|
|
|
String url = serverConfig.getUrl() + fileName;
|
|
|
@@ -119,6 +118,14 @@ public class CommonController
|
|
|
fileNames.add(fileName);
|
|
|
newFileNames.add(FileUtils.getName(fileName));
|
|
|
originalFilenames.add(file.getOriginalFilename());
|
|
|
+ // 2.开始新增文件上传信息
|
|
|
+ SysUploadFile sysFile = new SysUploadFile();
|
|
|
+ sysFile.setName(fileName);
|
|
|
+ sysFile.setPath(filePath);
|
|
|
+ sysFile.setUrl(url);
|
|
|
+ sysFile.setType(file.getContentType());
|
|
|
+ sysFile.setSize(file.getSize());
|
|
|
+ iSysUploadFileService.insertSysUploadFile(sysFile);
|
|
|
}
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
|
|
|
@@ -126,9 +133,7 @@ public class CommonController
|
|
|
ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
|
|
|
ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
|
|
|
return ajax;
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return AjaxResult.error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
@@ -138,12 +143,9 @@ public class CommonController
|
|
|
*/
|
|
|
@GetMapping("/download/resource")
|
|
|
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
|
|
|
- throws Exception
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- if (!FileUtils.checkAllowDownload(resource))
|
|
|
- {
|
|
|
+ throws Exception {
|
|
|
+ try {
|
|
|
+ if (!FileUtils.checkAllowDownload(resource)) {
|
|
|
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
|
|
|
}
|
|
|
// 本地资源路径
|
|
|
@@ -155,24 +157,22 @@ public class CommonController
|
|
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
|
|
FileUtils.setAttachmentResponseHeader(response, downloadName);
|
|
|
FileUtils.writeBytes(downloadPath, response.getOutputStream());
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
log.error("下载文件失败", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@PostMapping("/uploadMinio")
|
|
|
- public AjaxResult uploadFileMinio(MultipartFile file) throws Exception{
|
|
|
- try{
|
|
|
+ public AjaxResult uploadFileMinio(MultipartFile file) throws Exception {
|
|
|
+ try {
|
|
|
String fileName = FileUploadUtils.uploadMinio(file);
|
|
|
AjaxResult rt = AjaxResult.success();
|
|
|
- rt.put("url",fileName);
|
|
|
- rt.put("fileName",fileName);
|
|
|
- rt.put("newFileName",FileUtils.getName(fileName));
|
|
|
- rt.put("originalFileName",file.getOriginalFilename());
|
|
|
+ rt.put("url", fileName);
|
|
|
+ rt.put("fileName", fileName);
|
|
|
+ rt.put("newFileName", FileUtils.getName(fileName));
|
|
|
+ rt.put("originalFileName", file.getOriginalFilename());
|
|
|
return rt;
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
return AjaxResult.error(e.getMessage());
|
|
|
}
|
|
|
}
|