根据文件路径进行SFTP下载,上传
This commit is contained in:
parent
003c6a1fb5
commit
2a2581db3b
|
@ -110,12 +110,13 @@ public class SFTPUtil {
|
|||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param isConcat 是否拼接,true时拼接/kczx
|
||||
* @param directory 上传的目录
|
||||
* @param uploadFilePath 要上传的文件路径(文件真实路径+名称)
|
||||
* @param uploadFileName 要上传的文件名称(重新定义的文件名称)
|
||||
* @param
|
||||
*/
|
||||
public static Map<String,String> upload(SftpConfig sftpConfig, String directory, String uploadFilePath, String uploadFileName) {
|
||||
public static Map<String,String> upload(SftpConfig sftpConfig,boolean isConcat, String directory, String uploadFilePath, String uploadFileName) {
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
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 {
|
||||
|
|
|
@ -31,7 +31,7 @@ public interface IZykService extends IService<ZykInfo> {
|
|||
* @param filePath
|
||||
* @return 下载结果
|
||||
*/
|
||||
// Map<String,String> downloadRemoteFile(String filePath);
|
||||
Map<String,String> downloadRemoteFile(String filePath);
|
||||
|
||||
/**
|
||||
* 下载远端文件流(含接口)
|
||||
|
@ -45,6 +45,7 @@ public interface IZykService extends IService<ZykInfo> {
|
|||
* @return 上传结果
|
||||
*/
|
||||
Map<String,String> saveToZyk(ZykInfo zykInfo);
|
||||
Map<String,String> uploadFileToSftp(String filePath);
|
||||
|
||||
/**
|
||||
* 删除资源库(接口)
|
||||
|
|
|
@ -141,34 +141,34 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
* @param filePath
|
||||
* @return 下载结果
|
||||
*/
|
||||
// @Override
|
||||
// public Map<String,String> downloadRemoteFile(String filePath){
|
||||
// Map<String,String> map = new HashMap<String,String>();
|
||||
// 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<String,String> 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<String,String> downloadRemoteFile(String filePath){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
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<String,String> 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<ZykMapper, ZykInfo> implements I
|
|||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新到资源库(接口)
|
||||
* @param filePath
|
||||
* @return 上传结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String,String> uploadFileToSftp(String filePath){
|
||||
Map<String,String> map = fileHandleUtil.uploadFileToSftp(filePath);
|
||||
if(!map.get("code").equals("0")){
|
||||
return map;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除资源库(接口)
|
||||
* @param zykInfo
|
||||
|
|
|
@ -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<String,String> uploadFile(ZykInfo zykInfo){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
|
@ -43,7 +43,7 @@ public class FileHandleUtil {
|
|||
return fileNameMap;
|
||||
}
|
||||
//上传文件
|
||||
Map<String,String> uploadMap = SFTPUtil.upload(sftpConfig,directory,fileNameMap.get("filePath"),fileNameMap.get("fileName"));
|
||||
Map<String,String> 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<String,String> uploadMap = SFTPUtil.upload(sftpConfig,directory,fileNameMap.get("filePath"),fileNameMap.get("pdfName"));
|
||||
Map<String,String> 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<String,String> uploadFileToSftp(String filePath){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
if(!StringUtils.isEmpty(filePath)) {
|
||||
//源文件路径
|
||||
Map<String,String> fileNameMap = getFileName(filePath);
|
||||
if(!fileNameMap.get("code").equals("0")){
|
||||
return fileNameMap;
|
||||
}
|
||||
//上传文件
|
||||
Map<String,String> 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<String,String> download(String directory, String downloadFile){
|
||||
// return SFTPUtil.download(sftpConfig,directory,downloadFile,getDownloadPath("temp"));
|
||||
// }
|
||||
public Map<String,String> 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<String,String> getFileName(String fileNamePath){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue