diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/SFTPUtil.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/SFTPUtil.java index 38eb1ec5..37672874 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/SFTPUtil.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/SFTPUtil.java @@ -110,12 +110,13 @@ public class SFTPUtil { /** * 上传文件 + * @param isConcat 是否拼接,true时拼接/kczx * @param directory 上传的目录 * @param uploadFilePath 要上传的文件路径(文件真实路径+名称) * @param uploadFileName 要上传的文件名称(重新定义的文件名称) * @param */ - public static Map upload(SftpConfig sftpConfig, String directory, String uploadFilePath, String uploadFileName) { + public static Map upload(SftpConfig sftpConfig,boolean isConcat, String directory, String uploadFilePath, String uploadFileName) { Map map = new HashMap(); map.put("code","0"); map.put("msg","上传成功"); @@ -123,7 +124,9 @@ public class SFTPUtil { try{ sftp = connect(sftpConfig); try { - directory = getDirectory(sftpConfig.getUploadpath(),directory); + if(isConcat){ + directory = getDirectory(sftpConfig.getUploadpath(),directory); + } sftp.cd(directory); } catch (SftpException e1) { try { diff --git a/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/service/IZykService.java b/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/service/IZykService.java index 9b3c5c00..e9f45e55 100644 --- a/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/service/IZykService.java +++ b/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/service/IZykService.java @@ -31,7 +31,7 @@ public interface IZykService extends IService { * @param filePath * @return 下载结果 */ -// Map downloadRemoteFile(String filePath); + Map downloadRemoteFile(String filePath); /** * 下载远端文件流(含接口) @@ -45,6 +45,7 @@ public interface IZykService extends IService { * @return 上传结果 */ Map saveToZyk(ZykInfo zykInfo); + Map uploadFileToSftp(String filePath); /** * 删除资源库(接口) diff --git a/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/service/impl/ZykServiceImpl.java b/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/service/impl/ZykServiceImpl.java index 508a5149..651d9b47 100644 --- a/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/service/impl/ZykServiceImpl.java +++ b/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/service/impl/ZykServiceImpl.java @@ -141,34 +141,34 @@ public class ZykServiceImpl extends ServiceImpl implements I * @param filePath * @return 下载结果 */ -// @Override -// public Map downloadRemoteFile(String filePath){ -// Map map = new HashMap(); -// map.put("code","0"); -// if(filePath == null || filePath.equals("")){ -// map.put("code","1"); -// map.put("msg","文件路径不能为空!"); -// } -// if(filePath.lastIndexOf("/")==-1){ -// map.put("code","1"); -// map.put("msg","文件路径不存在!"); -// } -// String[] df = fileHandleUtil.getDirectoryAndFileName(filePath); -// if(df[0].equals("")){ -// map.put("code","1"); -// map.put("msg","文件路径不存在!"); -// } -// if(df[1].equals("")){ -// map.put("code","1"); -// map.put("msg","文件不存在!"); -// } -// Map downloadMap = fileHandleUtil.download(df[0],df[1]); -// if(!downloadMap.get("code").equals("0")){ -// return downloadMap; -// } -// map.put("data",downloadMap.get("fileName")); -// return map; -// } + @Override + public Map downloadRemoteFile(String filePath){ + Map map = new HashMap(); + map.put("code","0"); + if(filePath == null || filePath.equals("")){ + map.put("code","1"); + map.put("msg","文件路径不能为空!"); + } + if(filePath.lastIndexOf("/")==-1){ + map.put("code","1"); + map.put("msg","文件路径不存在!"); + } + String[] df = fileHandleUtil.getDirectoryAndFileName(filePath); + if(df[0].equals("")){ + map.put("code","1"); + map.put("msg","文件路径不存在!"); + } + if(df[1].equals("")){ + map.put("code","1"); + map.put("msg","文件不存在!"); + } + Map downloadMap = fileHandleUtil.download(df[0],df[1]); + if(!downloadMap.get("code").equals("0")){ + return downloadMap; + } + map.put("data",downloadMap.get("fileName")); + return map; + } /** * 下载远端文件流(含接口) @@ -225,6 +225,20 @@ public class ZykServiceImpl extends ServiceImpl implements I return map; } + /** + * 更新到资源库(接口) + * @param filePath + * @return 上传结果 + */ + @Override + public Map uploadFileToSftp(String filePath){ + Map map = fileHandleUtil.uploadFileToSftp(filePath); + if(!map.get("code").equals("0")){ + return map; + } + return map; + } + /** * 删除资源库(接口) * @param zykInfo diff --git a/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/utils/FileHandleUtil.java b/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/utils/FileHandleUtil.java index e44b7aff..dafee4c4 100644 --- a/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/utils/FileHandleUtil.java +++ b/jeecg-module-main/src/main/java/org/jeecg/modules/zyk/utils/FileHandleUtil.java @@ -23,8 +23,8 @@ public class FileHandleUtil { @Value(value = "${jeecg.path.upload}") private String uploadpath; -// @Value(value = "${jeecg.path.webapp}") -// private String downloadpath; + @Value(value = "${jeecg.path.webapp}") + private String downloadpath; public Map uploadFile(ZykInfo zykInfo){ Map map = new HashMap(); @@ -43,7 +43,7 @@ public class FileHandleUtil { return fileNameMap; } //上传文件 - Map uploadMap = SFTPUtil.upload(sftpConfig,directory,fileNameMap.get("filePath"),fileNameMap.get("fileName")); + Map uploadMap = SFTPUtil.upload(sftpConfig,true,directory,fileNameMap.get("filePath"),fileNameMap.get("fileName")); if(!uploadMap.get("code").equals("0")){ return uploadMap; } @@ -57,7 +57,7 @@ public class FileHandleUtil { return fileNameMap; } //上传文件 - Map uploadMap = SFTPUtil.upload(sftpConfig,directory,fileNameMap.get("filePath"),fileNameMap.get("pdfName")); + Map uploadMap = SFTPUtil.upload(sftpConfig,true,directory,fileNameMap.get("filePath"),fileNameMap.get("pdfName")); if(!uploadMap.get("code").equals("0")){ return uploadMap; } @@ -66,6 +66,27 @@ public class FileHandleUtil { return map; } + public Map uploadFileToSftp(String filePath){ + Map map = new HashMap(); + map.put("code","0"); + if(!StringUtils.isEmpty(filePath)) { + //源文件路径 + Map fileNameMap = getFileName(filePath); + if(!fileNameMap.get("code").equals("0")){ + return fileNameMap; + } + //上传文件 + Map uploadMap = SFTPUtil.upload(sftpConfig,false,"temp/",fileNameMap.get("filePath"),fileNameMap.get("fileName")); + if(!uploadMap.get("code").equals("0")){ + return uploadMap; + } + map.put("data",uploadMap.get("data")); + } + + return map; + } + + public String[] getDirectoryAndFileName(String fileName) { String[] ss = new String[2]; String path = fileName.substring(0,fileName.lastIndexOf("/")); @@ -75,9 +96,9 @@ public class FileHandleUtil { return ss; } -// public Map download(String directory, String downloadFile){ -// return SFTPUtil.download(sftpConfig,directory,downloadFile,getDownloadPath("temp")); -// } + public Map download(String directory, String downloadFile){ + return SFTPUtil.download(sftpConfig,directory,downloadFile,getDownloadPath("temp")); + } public void downloadRes(String directory, String downloadFile,HttpServletResponse response) throws Exception{ SFTPUtil.writeFileToRes(sftpConfig,downloadFile,response); @@ -251,6 +272,34 @@ public class FileHandleUtil { return map; } + /** + * 获取文件路径和新文件名称 + * @param fileNamePath + * @return + */ + private Map getFileName(String fileNamePath){ + Map map = new HashMap(); + map.put("code","0"); + String uploadFileName = getRealFileFullPath(fileNamePath); + if(StringUtils.isEmpty(uploadFileName)){ + map.put("code","1"); + map.put("msg","文件路径不能为空!"); + return map; + } + File file = new File(uploadFileName); + if(!file.exists()){ + map.put("code","1"); + map.put("msg","文件["+uploadFileName+"]不存在.."); + return map; + } + map.put("filePath",uploadFileName); + String fileName = uploadFileName.substring(uploadFileName.lastIndexOf('/')+1, uploadFileName.indexOf("_")); + String suffix = uploadFileName.substring(uploadFileName.lastIndexOf(".")); + fileName = fileName + suffix; + map.put("fileName",fileName); + return map; + } + /** * 获取文件名称 * @param zykInfo @@ -403,19 +452,18 @@ public class FileHandleUtil { * @param path * @return */ -// private String getDownloadPath(String path){ -// String filePath = ""; -// if(StringUtils.isEmpty(path)){ -// return ""; -// } -// int idx = path.indexOf(downloadpath); -// if(idx==-1){ -// filePath = downloadpath + File.separator + path; -// }else{ -// filePath = path; -// } -// return filePath; -// } - + private String getDownloadPath(String path){ + String filePath = ""; + if(StringUtils.isEmpty(path)){ + return ""; + } + int idx = path.indexOf(downloadpath); + if(idx==-1){ + filePath = downloadpath + File.separator + path; + }else{ + filePath = path; + } + return filePath; + } }