From 7a185c9ef8fea5e119d39d6ecede3572c2fe4e91 Mon Sep 17 00:00:00 2001 From: zhuyulei Date: Thu, 14 Sep 2023 11:26:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/CommonController.java | 39 +++++++++++++++---- .../src/main/resources/application-test.yml | 2 + .../jianwei/common/config/JianweiConfig.java | 11 ++++++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/jwtech-admin/src/main/java/com/kms/web/controller/common/CommonController.java b/jwtech-admin/src/main/java/com/kms/web/controller/common/CommonController.java index e2f73511..b312480c 100644 --- a/jwtech-admin/src/main/java/com/kms/web/controller/common/CommonController.java +++ b/jwtech-admin/src/main/java/com/kms/web/controller/common/CommonController.java @@ -1,11 +1,15 @@ package com.kms.web.controller.common; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.jianwei.common.utils.FileUploadUtils; +import com.kms.framework.config.ServerConfig; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,6 +44,9 @@ public class CommonController @Autowired private FastDfsUtil fastdfs; + @Autowired + private ServerConfig serverConfig; + /** * 通用下载请求 @@ -85,21 +92,37 @@ public class CommonController @PostMapping("/common/upload") public AjaxResult uploadFile(MultipartFile file) throws Exception { + String uploadType = JianweiConfig.getUploadType(); try { - String fileName = file.getOriginalFilename(); + if (uploadType.equals("FastDFS")) { + String fileName = file.getOriginalFilename(); + + // 上传并返回文件名称 + String uploadFile = fastdfs.uploadFile(file); + AjaxResult ajax = AjaxResult.success(); + ajax.put("fileName", fileName); + ajax.put("url", uploadFile); + return ajax; + } else { + // 上传文件路径 + String localPath = JianweiConfig.getProfile(); + // 上传并返回新文件名称 + String fileName = FileUploadUtils.upload(localPath, file); + String url = serverConfig.getUrl() + fileName; + AjaxResult ajax = AjaxResult.success(); + ajax.put("fileName", fileName); + ajax.put("url", url); + return ajax; - // 上传并返回文件名称 - String uploadFile = fastdfs.uploadFile(file); - AjaxResult ajax = AjaxResult.success(); - ajax.put("fileName", fileName); - ajax.put("url", uploadFile); - return ajax; + } } catch (Exception e) { + e.printStackTrace(); + return AjaxResult.error(e.getMessage()); } } @@ -121,6 +144,8 @@ public class CommonController response.setHeader("Content-Disposition", "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName)); FileUtils.writeBytes(downloadPath, response.getOutputStream()); + + } @PostMapping("/logout") diff --git a/jwtech-admin/src/main/resources/application-test.yml b/jwtech-admin/src/main/resources/application-test.yml index e30ed119..4d50c5e7 100644 --- a/jwtech-admin/src/main/resources/application-test.yml +++ b/jwtech-admin/src/main/resources/application-test.yml @@ -22,6 +22,8 @@ jianwei: #初始话数据的位置 initData: /home/project/tianhui-chonggou/data init: true + #上传类型 + uploadType: disk #neo4j的一个from database: tianhui_cg # 开发环境配置 diff --git a/jwtech-framework/src/main/java/com/jianwei/common/config/JianweiConfig.java b/jwtech-framework/src/main/java/com/jianwei/common/config/JianweiConfig.java index 8e574972..c474a844 100644 --- a/jwtech-framework/src/main/java/com/jianwei/common/config/JianweiConfig.java +++ b/jwtech-framework/src/main/java/com/jianwei/common/config/JianweiConfig.java @@ -48,6 +48,9 @@ public class JianweiConfig private static String detectLang; + + private static String uploadType; + public static String getDetectLang() { return detectLang; } @@ -109,11 +112,19 @@ public class JianweiConfig return profile; } + public static String getUploadType() { + return uploadType; + } + public void setProfile(String profile) { JianweiConfig.profile = profile; } + public void setUploadType(String uploadType) { + JianweiConfig.uploadType = uploadType; + } + public static boolean isAddressEnabled() { return addressEnabled;