Browse Source

文件上传下载

dev_kxc
zhuyulei 2 years ago
parent
commit
7a185c9ef8
  1. 25
      jwtech-admin/src/main/java/com/kms/web/controller/common/CommonController.java
  2. 2
      jwtech-admin/src/main/resources/application-test.yml
  3. 11
      jwtech-framework/src/main/java/com/jianwei/common/config/JianweiConfig.java

25
jwtech-admin/src/main/java/com/kms/web/controller/common/CommonController.java

@ -1,11 +1,15 @@
package com.kms.web.controller.common; package com.kms.web.controller.common;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -40,6 +44,9 @@ public class CommonController
@Autowired @Autowired
private FastDfsUtil fastdfs; private FastDfsUtil fastdfs;
@Autowired
private ServerConfig serverConfig;
/** /**
* 通用下载请求 * 通用下载请求
@ -85,10 +92,12 @@ public class CommonController
@PostMapping("/common/upload") @PostMapping("/common/upload")
public AjaxResult uploadFile(MultipartFile file) throws Exception public AjaxResult uploadFile(MultipartFile file) throws Exception
{ {
String uploadType = JianweiConfig.getUploadType();
try try
{ {
if (uploadType.equals("FastDFS")) {
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
// 上传并返回文件名称 // 上传并返回文件名称
@ -97,9 +106,23 @@ public class CommonController
ajax.put("fileName", fileName); ajax.put("fileName", fileName);
ajax.put("url", uploadFile); ajax.put("url", uploadFile);
return ajax; 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;
}
} }
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace();
return AjaxResult.error(e.getMessage()); return AjaxResult.error(e.getMessage());
} }
} }
@ -121,6 +144,8 @@ public class CommonController
response.setHeader("Content-Disposition", response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName)); "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
FileUtils.writeBytes(downloadPath, response.getOutputStream()); FileUtils.writeBytes(downloadPath, response.getOutputStream());
} }
@PostMapping("/logout") @PostMapping("/logout")

2
jwtech-admin/src/main/resources/application-test.yml

@ -22,6 +22,8 @@ jianwei:
#初始话数据的位置 #初始话数据的位置
initData: /home/project/tianhui-chonggou/data initData: /home/project/tianhui-chonggou/data
init: true init: true
#上传类型
uploadType: disk
#neo4j的一个from #neo4j的一个from
database: tianhui_cg database: tianhui_cg
# 开发环境配置 # 开发环境配置

11
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 detectLang;
private static String uploadType;
public static String getDetectLang() { public static String getDetectLang() {
return detectLang; return detectLang;
} }
@ -109,11 +112,19 @@ public class JianweiConfig
return profile; return profile;
} }
public static String getUploadType() {
return uploadType;
}
public void setProfile(String profile) public void setProfile(String profile)
{ {
JianweiConfig.profile = profile; JianweiConfig.profile = profile;
} }
public void setUploadType(String uploadType) {
JianweiConfig.uploadType = uploadType;
}
public static boolean isAddressEnabled() public static boolean isAddressEnabled()
{ {
return addressEnabled; return addressEnabled;

Loading…
Cancel
Save