();
- map.put("code","0");
- String uploadFileName = getRealFileFullPath(zykInfo.getFileName());
- if(uploadFileName.equals("")){
- map.put("code","1");
- map.put("msg","文件路径不能为空!");
- return map;
- }
- File file = new File(uploadFileName);
- if(!file.exists()){
- map.put("code","1");
- map.put("msg","文件["+zykInfo.getFileName()+"]不存在..");
- return map;
- }
- map.put("filePath",uploadFileName);
- String fileName = uploadFileName.substring(uploadFileName.lastIndexOf('/')+1, uploadFileName.indexOf("_"));
- String suffix = uploadFileName.substring(uploadFileName.lastIndexOf("."));
- fileName = fileName + suffix;
- if(zykInfo.getWjlx().equals(WjlxEnum.JXDG.getType())){
- String kcmc = zykInfo.getKcmc();
- fileName = kcmc+"_"+WjlxEnum.JXDG.getType()+suffix;
- }
- if(zykInfo.getWjlx().equals(WjlxEnum.JXRL.getType())){
- String kcmc = zykInfo.getKcmc();
- fileName = kcmc+"_"+WjlxEnum.JXRL.getType()+suffix;
- }
- if(zykInfo.getWjlx().equals(WjlxEnum.KCLW.getType())){
- String kcmc = zykInfo.getKcmc();
- fileName = kcmc+"_要求"+suffix;
- }
- if(zykInfo.getWjlx().equals(WjlxEnum.XSLW.getType())){
- String kcmc = zykInfo.getKcmc();
- String xh = zykInfo.getXh();
- String seq = zykInfo.getSeq();
- fileName = kcmc+"_"+xh+"_"+seq+suffix;
- }
- if(zykInfo.getWjlx().equals(WjlxEnum.JXDY.getType())){
- String kcmc = zykInfo.getKcmc();
- String chapter = zykInfo.getChapter();
- String section = zykInfo.getSection();
- fileName = kcmc+"_第"+chapter+"章_第"+section+"节"+suffix;
- }
- if(zykInfo.getWjlx().equals(WjlxEnum.QT.getType())){
- String kcmc = zykInfo.getKcmc();
- fileName = kcmc+"_其他_"+fileName;
- }
- map.put("fileName",fileName);
- return map;
- }
-
- /**
- * 获取文件真实路径
- * @param path
- * @return
- */
- private String getRealFileFullPath(String path){
- String filePath = "";
- if(path==null || path.equals("")){
- return "";
- }
- path = path.replace("\\", "/");
- int idx = path.indexOf(uploadpath);
- if(idx==-1){
- filePath = uploadpath + File.separator + path;
- }else{
- filePath = path;
- }
- return filePath;
- }
-
- /**
- * 获取文件真实路径
- * @param path
- * @return
- */
- private String getDownloadPath(String path){
- String filePath = "";
- if(path==null || path.equals("")){
- return "";
- }
- int idx = path.indexOf(downloadpath);
- if(idx==-1){
- filePath = downloadpath + File.separator + path;
- }else{
- filePath = path;
- }
- return filePath;
- }
-}
diff --git a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java
index 21e4bdfd..613de9bc 100644
--- a/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java
+++ b/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java
@@ -7,11 +7,9 @@ import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.exception.JeecgBootException;
-import org.jeecg.common.util.CommonUtils;
-import org.jeecg.common.util.RestUtil;
-import org.jeecg.common.util.TokenUtils;
+import org.jeecg.common.util.*;
import org.jeecg.common.util.filter.FileTypeFilter;
-import org.jeecg.common.util.oConvertUtils;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -29,6 +27,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
+import java.util.Map;
+
/**
*
* 用户表 前端控制器
@@ -51,6 +51,9 @@ public class CommonController {
@Value(value="${jeecg.uploadType}")
private String uploadType;
+ @Autowired
+ SftpConfig sftpConfig;
+
/**
* @Author 政辉
* @return
@@ -111,6 +114,9 @@ public class CommonController {
savePath = this.uploadLocal(file,bizPath);
}
*/
+ }if(CommonConstant.UPLOAD_TYPE_SFTP.equals(uploadType)) {
+ FileTypeFilter.fileTypeFilter(file);
+ savePath = this.uploadSftp(file,bizPath);
}else{
//update-begin-author:taoyan date:20200814 for:文件上传改造
savePath = CommonUtils.upload(file, bizPath, uploadType);
@@ -168,6 +174,18 @@ public class CommonController {
return "";
}
+ private String uploadSftp(MultipartFile mf,String bizPath){
+ try {
+ Map uploadMap = SFTPUtil.upload(sftpConfig,mf,bizPath);
+ if(uploadMap.get("code").equals("0")){
+ return uploadMap.get("data");
+ }
+ } catch (Exception e) {
+ log.error(e.getMessage(), e);
+ }
+ return "";
+ }
+
// @PostMapping(value = "/upload2")
// public Result> upload2(HttpServletRequest request, HttpServletResponse response) {
// Result> result = new Result<>();
@@ -227,24 +245,28 @@ public class CommonController {
if (imgPath.endsWith(SymbolConstant.COMMA)) {
imgPath = imgPath.substring(0, imgPath.length() - 1);
}
- String filePath = uploadpath + File.separator + imgPath;
- File file = new File(filePath);
- if(!file.exists()){
- response.setStatus(404);
- throw new RuntimeException("文件["+imgPath+"]不存在..");
+ if(CommonConstant.UPLOAD_TYPE_SFTP.equals(uploadType)) {
+ SFTPUtil.writeFileToRes(sftpConfig,imgPath,response);
+ }else {
+ String filePath = uploadpath + File.separator + imgPath;
+ File file = new File(filePath);
+ if (!file.exists()) {
+ response.setStatus(404);
+ throw new RuntimeException("文件[" + imgPath + "]不存在..");
+ }
+ // 设置强制下载不打开
+ response.setContentType("application/force-download");
+ response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
+ inputStream = new BufferedInputStream(new FileInputStream(filePath));
+ outputStream = response.getOutputStream();
+ byte[] buf = new byte[1024];
+ int len;
+ while ((len = inputStream.read(buf)) > 0) {
+ outputStream.write(buf, 0, len);
+ }
+ response.flushBuffer();
}
- // 设置强制下载不打开
- response.setContentType("application/force-download");
- response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
- inputStream = new BufferedInputStream(new FileInputStream(filePath));
- outputStream = response.getOutputStream();
- byte[] buf = new byte[1024];
- int len;
- while ((len = inputStream.read(buf)) > 0) {
- outputStream.write(buf, 0, len);
- }
- response.flushBuffer();
- } catch (IOException e) {
+ } catch (Exception e) {
log.error("预览文件失败" + e.getMessage());
response.setStatus(404);
e.printStackTrace();
diff --git a/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml b/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml
index 1adcfa57..094d7c3b 100644
--- a/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml
+++ b/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml
@@ -177,6 +177,7 @@ jeecg:
signUrls: /sys/dict/getDictItems/*,/sys/dict/loadDict/*,/sys/dict/loadDictOrderByValue/*,/sys/dict/loadDictItem/*,/sys/dict/loadTreeData,/sys/api/queryTableDictItemsByCode,/sys/api/queryFilterTableDictInfo,/sys/api/queryTableDictByKeys,/sys/api/translateDictFromTable,/sys/api/translateDictFromTableByKeys
#local、minio、alioss
uploadType: local
+# uploadType: sftp
# 前端访问地址
domainUrl:
pc: http://localhost:3100
@@ -344,4 +345,5 @@ sftp:
username: sftp
password: sftp
timeout: 1000
- uploadpath: /kczx
\ No newline at end of file
+ uploadpath: /kczx
+ fullpath: /home/sftp
\ No newline at end of file