SFTP上传优化
This commit is contained in:
parent
81a838bc03
commit
cd31ff7833
|
@ -19,59 +19,27 @@ import java.util.Properties;
|
|||
public class SFTPUtil {
|
||||
private static long count = 3;
|
||||
private static long count1 = 0;
|
||||
private static long sleepTime = 300000;//5分钟
|
||||
private static long sleepTime = 1800000;//30分钟
|
||||
private static final Logger logger = LoggerFactory.getLogger(SFTPUtil.class);
|
||||
private static Session sshSession = null;
|
||||
private static ChannelSftp sftp = null;
|
||||
|
||||
public static ChannelSftp connect(SftpConfig sftpConfig){
|
||||
ChannelSftp sftp = null;
|
||||
public static void getSession(SftpConfig sftpConfig){
|
||||
try {
|
||||
JSch jsch = new JSch();
|
||||
Session sshSession = jsch.getSession(sftpConfig.getUsername(), sftpConfig.getHostname(), sftpConfig.getPort());
|
||||
logger.info("Session created ... host=" + sftpConfig.getHostname() + ";port=" + sftpConfig.getPort()+";UserName=" + sftpConfig.getUsername() + ";Password="+sftpConfig.getPassword());
|
||||
if(sshSession == null ){
|
||||
JSch jsch = new JSch();
|
||||
sshSession = jsch.getSession(sftpConfig.getUsername(), sftpConfig.getHostname(), sftpConfig.getPort());
|
||||
logger.info("Session created ... host=" + sftpConfig.getHostname() + ";port=" + sftpConfig.getPort()+";UserName=" + sftpConfig.getUsername() + ";Password="+sftpConfig.getPassword());
|
||||
// jsch.addIdentity("e:/sftp_keys.ppk","");
|
||||
sshSession.setPassword(sftpConfig.getPassword());
|
||||
Properties sshConfig = new Properties();
|
||||
sshConfig.put("StrictHostKeyChecking", "no");
|
||||
sshSession.setConfig(sshConfig);
|
||||
sshSession.setTimeout(sftpConfig.getTimeout());
|
||||
sshSession.connect();
|
||||
logger.info("Session connected ...");
|
||||
logger.info("Opening Channel ...");
|
||||
Channel channel = sshSession.openChannel("sftp");
|
||||
channel.connect();
|
||||
sftp = (ChannelSftp) channel;
|
||||
logger.info("登录成功");
|
||||
} catch (Exception e){
|
||||
try{
|
||||
count1 += 1;
|
||||
if(count == count1){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Thread.sleep(sleepTime);
|
||||
logger.info("重新连接....");
|
||||
connect(sftpConfig);
|
||||
} catch (InterruptedException e1){
|
||||
throw new RuntimeException(e1);
|
||||
sshSession.setPassword(sftpConfig.getPassword());
|
||||
Properties sshConfig = new Properties();
|
||||
sshConfig.put("StrictHostKeyChecking", "no");
|
||||
sshSession.setConfig(sshConfig);
|
||||
sshSession.setTimeout(sftpConfig.getTimeout());
|
||||
sshSession.connect();
|
||||
logger.info("Session connected ...");
|
||||
logger.info("Opening Channel ...");
|
||||
}
|
||||
}
|
||||
return sftp;
|
||||
}
|
||||
|
||||
public static Session getSession(SftpConfig sftpConfig){
|
||||
Session sshSession = null;
|
||||
try {
|
||||
JSch jsch = new JSch();
|
||||
sshSession = jsch.getSession(sftpConfig.getUsername(), sftpConfig.getHostname(), sftpConfig.getPort());
|
||||
logger.info("Session created ... host=" + sftpConfig.getHostname() + ";port=" + sftpConfig.getPort()+";UserName=" + sftpConfig.getUsername() + ";Password="+sftpConfig.getPassword());
|
||||
// jsch.addIdentity("e:/sftp_keys.ppk","");
|
||||
sshSession.setPassword(sftpConfig.getPassword());
|
||||
Properties sshConfig = new Properties();
|
||||
sshConfig.put("StrictHostKeyChecking", "no");
|
||||
sshSession.setConfig(sshConfig);
|
||||
sshSession.setTimeout(sftpConfig.getTimeout());
|
||||
sshSession.connect();
|
||||
logger.info("Session connected ...");
|
||||
logger.info("Opening Channel ...");
|
||||
} catch (Exception e){
|
||||
try{
|
||||
count1 += 1;
|
||||
|
@ -85,10 +53,35 @@ public class SFTPUtil {
|
|||
throw new RuntimeException(e1);
|
||||
}
|
||||
}
|
||||
return sshSession;
|
||||
}
|
||||
|
||||
private static void mkdirs(String directory,ChannelSftp sftp) throws SftpException {
|
||||
public static void getChannelSftp(SftpConfig sftpConfig){
|
||||
try {
|
||||
if(sshSession == null){
|
||||
if(sshSession == null){
|
||||
getSession(sftpConfig);
|
||||
}
|
||||
Channel channel = sshSession.openChannel("sftp");
|
||||
channel.connect();
|
||||
sftp = (ChannelSftp) channel;
|
||||
logger.info("登录成功");
|
||||
}
|
||||
} catch (Exception e){
|
||||
try{
|
||||
count1 += 1;
|
||||
if(count == count1){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Thread.sleep(sleepTime);
|
||||
logger.info("重新连接....");
|
||||
getChannelSftp(sftpConfig);
|
||||
} catch (InterruptedException e1){
|
||||
throw new RuntimeException(e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void mkdirs(String directory) throws SftpException {
|
||||
String[] dics = directory.split("/");
|
||||
for(int i=0;i< dics.length;i++){
|
||||
try {
|
||||
|
@ -101,7 +94,7 @@ public class SFTPUtil {
|
|||
sftp.mkdir(dics[i]);
|
||||
sftp.cd(dics[i]);
|
||||
} catch (SftpException e2) {
|
||||
throw new RuntimeException("打开文件夹错误,[{}]", e2);
|
||||
throw new RuntimeException("打开文件夹错误,"+e2.getMessage(), e2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -115,14 +108,23 @@ public class SFTPUtil {
|
|||
* @param uploadFileName 要上传的文件名称(重新定义的文件名称)
|
||||
* @param
|
||||
*/
|
||||
public static Map<String,Object> upload(ChannelSftp sftp, SftpConfig sftpConfig,boolean isConcat, String directory, String uploadFilePath, String uploadFileName) {
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
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","上传成功");
|
||||
// ChannelSftp sftp = null;
|
||||
try{
|
||||
if(sftp == null){
|
||||
sftp = connect(sftpConfig);
|
||||
getChannelSftp(sftpConfig);
|
||||
}
|
||||
try {
|
||||
sftp.cd(sftpConfig.getFullpath());
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(sftpConfig.getFullpath());
|
||||
} catch (SftpException e2) {
|
||||
map.put("code", "1");
|
||||
map.put("msg", "sftp创建" + sftpConfig.getUploadpath() + "文件路径失败");
|
||||
}
|
||||
}
|
||||
try {
|
||||
if(isConcat){
|
||||
|
@ -131,7 +133,7 @@ public class SFTPUtil {
|
|||
sftp.cd(directory);
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(directory,sftp);
|
||||
mkdirs(directory);
|
||||
// sftp.mkdir(directory);
|
||||
// sftp.cd(directory);
|
||||
} catch (SftpException e2) {
|
||||
|
@ -156,10 +158,6 @@ public class SFTPUtil {
|
|||
}catch (Exception e) {
|
||||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
// if(sftp!=null){
|
||||
// disConnect(sftp);
|
||||
// }
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
@ -168,17 +166,25 @@ public class SFTPUtil {
|
|||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
map.put("msg","上传成功");
|
||||
ChannelSftp sftp = null;
|
||||
try{
|
||||
sftp = connect(sftpConfig);
|
||||
if(sftp == null){
|
||||
getChannelSftp(sftpConfig);
|
||||
}
|
||||
try {
|
||||
sftp.cd(sftpConfig.getFullpath());
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(sftpConfig.getFullpath());
|
||||
} catch (SftpException e2) {
|
||||
map.put("code", "1");
|
||||
map.put("msg", "sftp创建" + sftpConfig.getUploadpath() + "文件路径失败");
|
||||
}
|
||||
}
|
||||
try {
|
||||
// directory = getDirectory(sftpConfig.getUploadpath(), directory);
|
||||
sftp.cd(directory);
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(directory,sftp);
|
||||
// sftp.mkdir(directory);
|
||||
// sftp.cd(directory);
|
||||
mkdirs(directory);
|
||||
} catch (SftpException e2) {
|
||||
map.put("code","1");
|
||||
map.put("msg","ftp创建"+directory+"文件路径失败");
|
||||
|
@ -208,10 +214,6 @@ public class SFTPUtil {
|
|||
}catch (Exception e) {
|
||||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
if(sftp!=null){
|
||||
disConnect(sftp);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
@ -223,15 +225,13 @@ public class SFTPUtil {
|
|||
* @param downloadFile 下载的文件
|
||||
* @param saveFile 存在本地的路径
|
||||
*/
|
||||
public static Map<String,Object> download(ChannelSftp sftp,SftpConfig sftpConfig, String directory, String downloadFile, String saveFile) {
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
public static Map<String,String> download(SftpConfig sftpConfig, String directory, String downloadFile, String saveFile) {
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
map.put("msg","删除成功");
|
||||
// ChannelSftp sftp = null;
|
||||
try{
|
||||
if(sftp == null){
|
||||
sftp = connect(sftpConfig);
|
||||
map.put("sftp",sftp);
|
||||
getChannelSftp(sftpConfig);
|
||||
}
|
||||
OutputStream output = null;
|
||||
try {
|
||||
|
@ -243,14 +243,14 @@ public class SFTPUtil {
|
|||
if (logger.isInfoEnabled()) {
|
||||
logger.info("开始获取远程文件:[{}]---->[{}]", new Object[]{directory, saveFile});
|
||||
}
|
||||
sftp.cd(directory);
|
||||
// sftp.cd(directory);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("打开远程文件:[{}]", new Object[]{directory});
|
||||
}
|
||||
String newName = CommonUtils.getFileName(downloadFile);
|
||||
String newDownloadFile = saveFile.concat(File.separator).concat(newName);
|
||||
output = new FileOutputStream(new File(newDownloadFile));
|
||||
sftp.get(downloadFile, output);
|
||||
sftp.get(directory, output);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("文件下载成功");
|
||||
}
|
||||
|
@ -266,10 +266,6 @@ public class SFTPUtil {
|
|||
}catch (Exception e) {
|
||||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
// if(sftp!=null){
|
||||
// disConnect(sftp);
|
||||
// }
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
@ -284,14 +280,15 @@ public class SFTPUtil {
|
|||
String[] df = getDirectoryAndFileName(downloadFilePath);
|
||||
String directory = df[0];
|
||||
String downloadFile = df[1];
|
||||
ChannelSftp sftp = null;
|
||||
try{
|
||||
sftp = connect(sftpConfig);
|
||||
if(sftp == null){
|
||||
getChannelSftp(sftpConfig);
|
||||
}
|
||||
InputStream inputStream = null;
|
||||
ServletOutputStream outputStream=null;
|
||||
try {
|
||||
sftp.cd(directory);
|
||||
inputStream = sftp.get(downloadFile);
|
||||
// sftp.cd(directory);
|
||||
inputStream = sftp.get(downloadFilePath);
|
||||
byte[] buf = new byte[1024 * 10];
|
||||
outputStream = response.getOutputStream();
|
||||
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(downloadFile, "UTF-8"));
|
||||
|
@ -300,76 +297,18 @@ public class SFTPUtil {
|
|||
outputStream.write(buf, 0, readLength);
|
||||
}
|
||||
outputStream.flush();
|
||||
} catch (SftpException e1) {
|
||||
e1.printStackTrace();
|
||||
throw new Exception(StringUtils.format("sftp exception,sftp exception。 ", e1.getMessage()));
|
||||
} catch (IOException e2) {
|
||||
// e2.printStackTrace();
|
||||
// throw new Exception(StringUtils.format("io exception,io exception 。 ", e2.getMessage()));
|
||||
}finally {
|
||||
closeStream(null,outputStream);
|
||||
try {
|
||||
if(inputStream!=null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException e3) {
|
||||
e3.printStackTrace();
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
// e1.printStackTrace();
|
||||
// throw new Exception(StringUtils.format("sftp exception,sftp exception。 ", e1.getMessage()));
|
||||
} finally {
|
||||
closeStream(inputStream,outputStream);
|
||||
}
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new Exception(StringUtils.format("sftp exception,sftp exception。 ", e.getMessage()));
|
||||
}finally {
|
||||
if(sftp!=null){
|
||||
disConnect(sftp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 上传文件
|
||||
// * @param oldpath 原文件路径(文件路径+名称)
|
||||
// * @param directory 目的文件路径
|
||||
// * @param newname 目的文件名称
|
||||
// * @param
|
||||
// */
|
||||
// public static Map<String,String> rename(SftpConfig sftpConfig, String oldpath, String directory, String newname) {
|
||||
// Map<String,String> map = new HashMap<String,String>();
|
||||
// map.put("code","0");
|
||||
// map.put("msg","上传成功");
|
||||
// ChannelSftp sftp = null;
|
||||
// try{
|
||||
// sftp = connect(sftpConfig);
|
||||
// try {
|
||||
// directory = getDirectory(sftpConfig.getUploadpath(),directory);
|
||||
// sftp.cd(directory);
|
||||
// } catch (SftpException e1) {
|
||||
// try {
|
||||
// mkdirs(directory,sftp);
|
||||
// } catch (SftpException e2) {
|
||||
// map.put("code","1");
|
||||
// map.put("msg","ftp创建"+directory+"文件路径失败");
|
||||
// }
|
||||
// }
|
||||
// try {
|
||||
// String newpath = directory.concat(newname);
|
||||
// sftp.rename(oldpath,newpath);
|
||||
// } catch (SftpException e1) {
|
||||
// map.put("code","1");
|
||||
// map.put("msg",e1.getMessage());
|
||||
//// throw new RuntimeException("ftp创建文件路径失败" + directory);
|
||||
// }
|
||||
// }catch (Exception e) {
|
||||
// map.put("code","1");
|
||||
// map.put("msg","sftp异常:" + e.getMessage());
|
||||
// }finally {
|
||||
// if(sftp!=null){
|
||||
// disConnect(sftp);
|
||||
// }
|
||||
// }
|
||||
// return map;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 移动文件
|
||||
* @param oldpath 原文件路径(文件路径+名称)
|
||||
|
@ -377,19 +316,30 @@ public class SFTPUtil {
|
|||
* @param newname 目的文件名称
|
||||
* @param
|
||||
*/
|
||||
public static Map<String,Object> moveFile(SftpConfig sftpConfig, String oldpath, String directory, String newname) {
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
public static Map<String,String> moveFile(SftpConfig sftpConfig, String oldpath, String directory, String newname) {
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
map.put("msg","上传成功");
|
||||
ChannelSftp sftp = null;
|
||||
try {
|
||||
sftp = connect(sftpConfig);
|
||||
if(sftp == null){
|
||||
getChannelSftp(sftpConfig);
|
||||
}
|
||||
try {
|
||||
sftp.cd(sftpConfig.getFullpath());
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(sftpConfig.getFullpath());
|
||||
} catch (SftpException e2) {
|
||||
map.put("code", "1");
|
||||
map.put("msg", "sftp创建" + sftpConfig.getUploadpath() + "文件路径失败");
|
||||
}
|
||||
}
|
||||
try {
|
||||
directory = getDirectory(sftpConfig.getUploadpath(), directory);
|
||||
sftp.cd(directory);
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(directory, sftp);
|
||||
mkdirs(directory);
|
||||
} catch (SftpException e2) {
|
||||
map.put("code", "1");
|
||||
map.put("msg", "ftp创建" + directory + "文件路径失败");
|
||||
|
@ -404,8 +354,7 @@ public class SFTPUtil {
|
|||
String newpath = directory.concat(newname);
|
||||
String newfullpath = sftpConfig.getFullpath().concat(newpath);
|
||||
String moveCommand = "mv " + oldfullpath + " " + newfullpath; // 移动文件的命令
|
||||
Session session = sftp.getSession();
|
||||
channel = session.openChannel("exec");
|
||||
channel = sshSession.openChannel("exec");
|
||||
((ChannelExec) channel).setCommand(moveCommand);
|
||||
channel.setInputStream(null);
|
||||
((ChannelExec) channel).setErrStream(System.err);
|
||||
|
@ -427,7 +376,7 @@ public class SFTPUtil {
|
|||
}
|
||||
}
|
||||
channel.disconnect();
|
||||
session.disconnect();
|
||||
channel = null;
|
||||
map.put("data",newpath);
|
||||
} catch (Exception e1) {
|
||||
map.put("code","1");
|
||||
|
@ -436,119 +385,46 @@ public class SFTPUtil {
|
|||
}finally {
|
||||
if(channel!=null){
|
||||
channel.disconnect();
|
||||
channel = null;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
if(sftp!=null){
|
||||
disConnect(sftp);
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Map<String,Object> moveFiles(SftpConfig sftpConfig, String directory, List<String[]> list) {
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
map.put("code","0");
|
||||
map.put("msg","上传成功");
|
||||
ChannelSftp sftp = null;
|
||||
try {
|
||||
sftp = connect(sftpConfig);
|
||||
try {
|
||||
directory = getDirectory(sftpConfig.getUploadpath(), directory);
|
||||
sftp.cd(directory);
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(directory, sftp);
|
||||
} catch (SftpException e2) {
|
||||
map.put("code", "1");
|
||||
map.put("msg", "ftp创建" + directory + "文件路径失败");
|
||||
}
|
||||
}
|
||||
Channel channel = null;
|
||||
try {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for(int item=0;item<list.size();item++){
|
||||
String oldpath = list.get(item)[0];
|
||||
if(!oldpath.startsWith("/")){
|
||||
oldpath = "/"+oldpath;
|
||||
}
|
||||
String oldfullpath = sftpConfig.getFullpath().concat(oldpath);
|
||||
String newname = list.get(item)[1];
|
||||
String newpath = directory.concat(newname);
|
||||
String newfullpath = sftpConfig.getFullpath().concat(newpath);
|
||||
String moveCommand = "mv " + oldfullpath + " " + newfullpath; // 移动文件的命令
|
||||
Session session = sftp.getSession();
|
||||
channel = session.openChannel("exec");
|
||||
((ChannelExec) channel).setCommand(moveCommand);
|
||||
channel.setInputStream(null);
|
||||
((ChannelExec) channel).setErrStream(System.err);
|
||||
InputStream in = channel.getInputStream();
|
||||
channel.connect();
|
||||
byte[] tmp = new byte[1024];
|
||||
while (true) {
|
||||
while (in.available() > 0) {
|
||||
int i = in.read(tmp, 0, 1024);
|
||||
if (i < 0) break;
|
||||
}
|
||||
if (channel.isClosed()) {
|
||||
System.out.println("exit-status: " + channel.getExitStatus());
|
||||
break;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (Exception ee) {
|
||||
}
|
||||
}
|
||||
channel.disconnect();
|
||||
sb.append(newpath);
|
||||
sb.append("|");
|
||||
}
|
||||
String path = sb.toString();
|
||||
path = path.substring(0,path.length()-1);
|
||||
map.put("data",path);
|
||||
} catch (Exception e1) {
|
||||
map.put("code","1");
|
||||
map.put("msg",e1.getMessage());
|
||||
// throw new RuntimeException("ftp创建文件路径失败" + directory);
|
||||
}finally {
|
||||
if(channel!=null){
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
if(sftp!=null){
|
||||
disConnect(sftp);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Map<String,Object> moveFiles(ChannelSftp sftp,SftpConfig sftpConfig, String directory, List<String[]> list) {
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
public static Map<String,String> moveFiles(SftpConfig sftpConfig, String directory, List<String[]> list) {
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
map.put("msg","上传成功");
|
||||
try {
|
||||
if(sftp == null){
|
||||
sftp = connect(sftpConfig);
|
||||
getChannelSftp(sftpConfig);
|
||||
}
|
||||
try {
|
||||
sftp.cd(sftpConfig.getFullpath());
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(sftpConfig.getFullpath());
|
||||
} catch (SftpException e2) {
|
||||
map.put("code", "1");
|
||||
map.put("msg", "sftp创建" + sftpConfig.getUploadpath() + "文件路径失败");
|
||||
}
|
||||
}
|
||||
try {
|
||||
directory = getDirectory(sftpConfig.getUploadpath(), directory);
|
||||
sftp.cd(directory);
|
||||
} catch (SftpException e1) {
|
||||
try {
|
||||
mkdirs(directory, sftp);
|
||||
mkdirs(directory);
|
||||
} catch (SftpException e2) {
|
||||
map.put("code", "1");
|
||||
map.put("msg", "ftp创建" + directory + "文件路径失败");
|
||||
map.put("msg", "sftp创建" + directory + "文件路径失败");
|
||||
}
|
||||
}
|
||||
sftp.cd(sftpConfig.getFullpath());
|
||||
Channel channel = null;
|
||||
try {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
@ -560,10 +436,9 @@ public class SFTPUtil {
|
|||
String oldfullpath = sftpConfig.getFullpath().concat(oldpath);
|
||||
String newname = list.get(item)[1];
|
||||
String newpath = directory.concat(newname);
|
||||
String newfullpath = sftpConfig.getFullpath().concat(newpath);
|
||||
String newfullpath = sftpConfig.getFullpath().concat("/").concat(newpath);
|
||||
String moveCommand = "mv " + oldfullpath + " " + newfullpath; // 移动文件的命令
|
||||
Session session = sftp.getSession();
|
||||
channel = session.openChannel("exec");
|
||||
channel = sshSession.openChannel("exec");
|
||||
((ChannelExec) channel).setCommand(moveCommand);
|
||||
channel.setInputStream(null);
|
||||
((ChannelExec) channel).setErrStream(System.err);
|
||||
|
@ -585,6 +460,7 @@ public class SFTPUtil {
|
|||
}
|
||||
}
|
||||
channel.disconnect();
|
||||
channel = null;
|
||||
sb.append(newpath);
|
||||
sb.append("|");
|
||||
}
|
||||
|
@ -598,17 +474,17 @@ public class SFTPUtil {
|
|||
}finally {
|
||||
if(channel!=null){
|
||||
channel.disconnect();
|
||||
channel = null;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param directory 要删除文件所在目录
|
||||
|
@ -618,19 +494,16 @@ public class SFTPUtil {
|
|||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
map.put("msg","删除成功");
|
||||
ChannelSftp sftp = null;
|
||||
try{
|
||||
sftp = connect(sftpConfig);
|
||||
if(sftp == null){
|
||||
getChannelSftp(sftpConfig);
|
||||
}
|
||||
directory = delDiagonalLines(directory);
|
||||
sftp.cd(directory);
|
||||
sftp.rm(deleteFile);
|
||||
}catch (Exception e) {
|
||||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
if(sftp!=null){
|
||||
disConnect(sftp);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
@ -654,10 +527,23 @@ public class SFTPUtil {
|
|||
/**
|
||||
* 断掉连接
|
||||
*/
|
||||
public static void disConnect(ChannelSftp sftp) {
|
||||
public static void disChannel() {
|
||||
try {
|
||||
sftp.disconnect();
|
||||
sftp.getSession().disconnect();
|
||||
if(sftp!=null){
|
||||
sftp.disconnect();
|
||||
sftp=null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void disSession() {
|
||||
try {
|
||||
if(sshSession!=null) {
|
||||
sshSession.disconnect();
|
||||
sshSession=null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -673,6 +559,7 @@ public class SFTPUtil {
|
|||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
}
|
||||
}
|
||||
if(inputStream != null){
|
||||
|
@ -680,6 +567,7 @@ public class SFTPUtil {
|
|||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.jeecg.modules.kc.zyJxdg.controller;
|
|||
import cn.hutool.core.io.FileUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
|
@ -312,6 +313,11 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
|
|||
@ApiOperation(value="教学大纲-上传附件", notes="教学大纲-上传附件")
|
||||
@PostMapping(value = "/jxdgScfj")
|
||||
public Result<ZyJxdg> jxdgScfj(@RequestBody ZyJxdg zyJxdg) {
|
||||
boolean isAdd = false;
|
||||
if(StringUtils.isEmpty(zyJxdg.getId())){
|
||||
isAdd = true;
|
||||
zyJxdg.setId((new DefaultIdentifierGenerator().nextId(new ZyJxdg())).toString());
|
||||
}
|
||||
if(StringUtils.isNotEmpty(zyJxdg.getFilePath())){
|
||||
String nameLast = zyJxdg.getFilePath().substring(zyJxdg.getFilePath().lastIndexOf(".")+1);
|
||||
if(StringUtils.equals("pdf",nameLast)){
|
||||
|
@ -334,12 +340,11 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
|
|||
zykInfo.setWjlx(WjlxEnum.JXDG.getType());
|
||||
zykInfo.setRwbh(zyJxdg.getRwbh());
|
||||
zykInfo.setFileName(zyJxdg.getFilePath());
|
||||
Map<String,Object> map = zykService.saveToZykJxdg(zykInfo);
|
||||
String fileName = map.get("data").toString();
|
||||
String pdfName = map.get("pdfData").toString();
|
||||
Map<String,String> map = zykService.saveToZykJxdg(zykInfo);
|
||||
String fileName = map.get("data");
|
||||
String pdfName = map.get("pdfData");
|
||||
zyJxdg.setFilePath(fileName);
|
||||
zyJxdg.setPdfPath(pdfName);
|
||||
|
||||
}
|
||||
}else{
|
||||
zyJxdg.setPdfPath(null);
|
||||
|
@ -363,8 +368,7 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
|
|||
//--------上传ftp工具---------------
|
||||
|
||||
|
||||
|
||||
if(StringUtils.isEmpty(zyJxdg.getId())){
|
||||
if(isAdd){
|
||||
zyJxdgService.save(zyJxdg);
|
||||
}else{
|
||||
UpdateWrapper<ZyJxdg> updateWrapper = new UpdateWrapper<>();
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
update_time,
|
||||
biz_id,
|
||||
biz_table,
|
||||
rwbh,
|
||||
xqxn,
|
||||
kkdw,
|
||||
kcmc,
|
||||
|
@ -52,6 +53,7 @@
|
|||
update_time,
|
||||
biz_id,
|
||||
biz_table,
|
||||
rwbh,
|
||||
xqxn,
|
||||
kkdw,
|
||||
kcmc,
|
||||
|
@ -74,6 +76,7 @@
|
|||
create_time,
|
||||
biz_id,
|
||||
biz_table,
|
||||
rwbh,
|
||||
xqxn,
|
||||
kkdw,
|
||||
kcmc,
|
||||
|
@ -92,6 +95,7 @@
|
|||
#{createTime},
|
||||
#{bizId},
|
||||
#{bizTable},
|
||||
#{rwbh},
|
||||
#{xqxn},
|
||||
#{kkdw},
|
||||
#{kcmc},
|
||||
|
@ -129,6 +133,7 @@
|
|||
update_time,
|
||||
biz_id,
|
||||
biz_table,
|
||||
rwbh,
|
||||
xqxn,
|
||||
kkdw,
|
||||
kcmc,
|
||||
|
|
|
@ -31,7 +31,7 @@ public interface IZykService extends IService<ZykInfo> {
|
|||
* @param filePath
|
||||
* @return 下载结果
|
||||
*/
|
||||
Map<String,Object> downloadRemoteFile(String filePath);
|
||||
Map<String,String> downloadRemoteFile(String filePath);
|
||||
|
||||
/**
|
||||
* 下载远端文件流(含接口)
|
||||
|
@ -44,9 +44,9 @@ public interface IZykService extends IService<ZykInfo> {
|
|||
* @param zykInfo
|
||||
* @return 上传结果
|
||||
*/
|
||||
Map<String,Object> saveToZyk(ZykInfo zykInfo);
|
||||
Map<String,Object> saveToZykJxdg(ZykInfo zykInfo);
|
||||
Map<String,Object> uploadFileToSftp(Map<String,Object> map,String filePath);
|
||||
Map<String,String> saveToZyk(ZykInfo zykInfo);
|
||||
Map<String,String> saveToZykJxdg(ZykInfo zykInfo);
|
||||
Map<String,String> uploadFileToSftp(String filePath);
|
||||
|
||||
/**
|
||||
* 删除资源库(接口)
|
||||
|
|
|
@ -56,16 +56,16 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
@Override
|
||||
public String saveInfo(ZykInfo zykInfo){
|
||||
// Map<String,String> map = fileHandleUtil.uploadFile(zykInfo);
|
||||
Map<String,Object> map = fileHandleUtil.moveFiles(null,zykInfo);
|
||||
Map<String,String> map = fileHandleUtil.moveFiles(zykInfo);
|
||||
if(!map.get("code").equals("0")){
|
||||
return map.get("msg").toString();
|
||||
}
|
||||
if(!StringUtils.isEmpty(map.get("data").toString())){
|
||||
String fileName = map.get("data").toString();
|
||||
if(StringUtils.isNotNull(map.get("data"))){
|
||||
String fileName = map.get("data");
|
||||
zykInfo.setFileName(fileName);
|
||||
}
|
||||
if(!StringUtils.isEmpty(map.get("pdfData").toString())){
|
||||
String pdfName = map.get("pdfData").toString();
|
||||
if(StringUtils.isNotNull(map.get("pdfData"))){
|
||||
String pdfName = map.get("pdfData");
|
||||
zykInfo.setPdfName(pdfName);
|
||||
}
|
||||
LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||
|
@ -80,16 +80,16 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
@Override
|
||||
public String modifyInfo(ZykInfo zykInfo){
|
||||
// Map<String,String> map = fileHandleUtil.uploadFile(zykInfo);
|
||||
Map<String,Object> map = fileHandleUtil.moveFiles(null,zykInfo);
|
||||
Map<String,String> map = fileHandleUtil.moveFiles(zykInfo);
|
||||
if(!map.get("code").equals("0")){
|
||||
return map.get("msg").toString();
|
||||
return map.get("msg");
|
||||
}
|
||||
if(!StringUtils.isEmpty(map.get("data").toString())){
|
||||
String fileName = map.get("data").toString();
|
||||
if(StringUtils.isNotNull(map.get("data"))){
|
||||
String fileName = map.get("data");
|
||||
zykInfo.setFileName(fileName);
|
||||
}
|
||||
if(!StringUtils.isEmpty(map.get("pdfData").toString())){
|
||||
String pdfName = map.get("pdfData").toString();
|
||||
if(StringUtils.isNotNull(map.get("pdfData"))){
|
||||
String pdfName = map.get("pdfData");
|
||||
zykInfo.setPdfName(pdfName);
|
||||
}
|
||||
LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||
|
@ -114,6 +114,7 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
fileHandleUtil.delete(df[0],df[1]);
|
||||
}
|
||||
}
|
||||
fileHandleUtil.disConnect();
|
||||
zykMapper.deleteById(id);
|
||||
return "删除成功!";
|
||||
}
|
||||
|
@ -137,6 +138,7 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
}
|
||||
zykMapper.deleteById(id);
|
||||
}
|
||||
fileHandleUtil.disConnect();
|
||||
return "批量删除成功!";
|
||||
}
|
||||
|
||||
|
@ -146,8 +148,8 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
* @return 下载结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String,Object> downloadRemoteFile(String filePath){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
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");
|
||||
|
@ -166,12 +168,11 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
map.put("code","1");
|
||||
map.put("msg","文件不存在!");
|
||||
}
|
||||
Map<String,Object> downloadMap = fileHandleUtil.download(null,df[0],df[1]);
|
||||
Map<String,String> downloadMap = fileHandleUtil.download(filePath,df[1]);
|
||||
if(!downloadMap.get("code").equals("0")){
|
||||
return downloadMap;
|
||||
}
|
||||
map.put("data",downloadMap.get("fileName"));
|
||||
map.put("sftp",downloadMap.get("sftp"));
|
||||
return map;
|
||||
}
|
||||
|
||||
|
@ -187,11 +188,7 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
if(filePath.lastIndexOf("/")==-1){
|
||||
throw new RuntimeException("文件路径不存在!");
|
||||
}
|
||||
String[] df = fileHandleUtil.getDirectoryAndFileName(filePath);
|
||||
if(StringUtils.isEmpty(df[0])||StringUtils.isEmpty(df[1])){
|
||||
throw new RuntimeException("文件路径不存在!");
|
||||
}
|
||||
fileHandleUtil.downloadRes(df[0],df[1],response);
|
||||
fileHandleUtil.downloadRes(filePath,response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -200,7 +197,7 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
* @return 上传结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String,Object> saveToZyk(ZykInfo zykInfo){
|
||||
public Map<String,String> saveToZyk(ZykInfo zykInfo){
|
||||
KcKechengbiao kcb = new KcKechengbiao();
|
||||
kcb.setRwbh(zykInfo.getRwbh());
|
||||
kcb = kcKechengbiaoService.getKcbhByRwbh(kcb);
|
||||
|
@ -209,16 +206,16 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
zykInfo.setKcmc(kcb.getKcmc());
|
||||
zykInfo.setSkjs(kcb.getSkjs());
|
||||
// Map<String,Object> map = fileHandleUtil.uploadFile(zykInfo);
|
||||
Map<String,Object> map = fileHandleUtil.moveFiles(null,zykInfo);
|
||||
Map<String,String> map = fileHandleUtil.moveFiles(zykInfo);
|
||||
if(!map.get("code").equals("0")){
|
||||
return map;
|
||||
}
|
||||
if(!StringUtils.isEmpty(map.get("data").toString())){
|
||||
String fileName = map.get("data").toString();
|
||||
if(StringUtils.isNotNull(map.get("data"))){
|
||||
String fileName = map.get("data");
|
||||
zykInfo.setFileName(fileName);
|
||||
}
|
||||
if(!StringUtils.isEmpty(map.get("pdfData").toString())){
|
||||
String pdfName = map.get("pdfData").toString();
|
||||
if(StringUtils.isNotNull(map.get("pdfData"))){
|
||||
String pdfName = map.get("pdfData");
|
||||
zykInfo.setPdfName(pdfName);
|
||||
}
|
||||
LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||
|
@ -236,7 +233,7 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
* @return 上传结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String,Object> saveToZykJxdg(ZykInfo zykInfo){
|
||||
public Map<String,String> saveToZykJxdg(ZykInfo zykInfo){
|
||||
KcKechengbiao kcb = new KcKechengbiao();
|
||||
kcb.setRwbh(zykInfo.getRwbh());
|
||||
kcb = kcKechengbiaoService.getKcbhByRwbh(kcb);
|
||||
|
@ -244,32 +241,37 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
zykInfo.setKkdw(kcb.getKkdw());
|
||||
zykInfo.setKcmc(kcb.getKcmc());
|
||||
zykInfo.setSkjs(kcb.getSkjs());
|
||||
Map<String,Object> downloadMap = downloadRemoteFile(zykInfo.getFileName());
|
||||
Map<String,String> downloadMap = downloadRemoteFile(zykInfo.getFileName());
|
||||
if(!downloadMap.get("code").equals("0")){
|
||||
return downloadMap;
|
||||
}
|
||||
Global global = SpringContextHolder.getBean(Global.class);
|
||||
String dd = DateUtils.formatDate(new Date(),"yyyyMMddHHmmss");
|
||||
String namePath = dd + ".pdf";
|
||||
String pdfPath = global.getContractDickPath() + namePath;
|
||||
if(!downloadMap.get("code").equals("0")){
|
||||
return downloadMap;
|
||||
}
|
||||
PDFUtil.office2PDF(downloadMap.get("data").toString(),pdfPath);
|
||||
Map<String,Object> uploadMap = uploadFileToSftp(downloadMap,"contract/"+namePath);
|
||||
|
||||
PDFUtil.office2PDF(downloadMap.get("data"),pdfPath);
|
||||
Map<String,String> uploadMap = uploadFileToSftp("contract/"+namePath);
|
||||
if(!uploadMap.get("code").equals("0")){
|
||||
return uploadMap;
|
||||
}
|
||||
zykInfo.setPdfName(uploadMap.get("data").toString());
|
||||
zykInfo.setPdfName(uploadMap.get("data"));
|
||||
|
||||
Map<String,Object> map = fileHandleUtil.moveFiles(downloadMap,zykInfo);
|
||||
Map<String,String> map = fileHandleUtil.moveFiles(zykInfo);
|
||||
if(!map.get("code").equals("0")){
|
||||
return map;
|
||||
}
|
||||
if(!StringUtils.isEmpty(map.get("data").toString())){
|
||||
String fileName = map.get("data").toString();
|
||||
if(StringUtils.isNotNull(map.get("data"))){
|
||||
String fileName = map.get("data");
|
||||
zykInfo.setFileName(fileName);
|
||||
}else{
|
||||
map.put("data","");
|
||||
}
|
||||
if(!StringUtils.isEmpty(map.get("pdfData").toString())){
|
||||
String pdfName = map.get("pdfData").toString();
|
||||
if(StringUtils.isNotNull(map.get("pdfData"))){
|
||||
String pdfName = map.get("pdfData");
|
||||
zykInfo.setPdfName(pdfName);
|
||||
}else{
|
||||
map.put("pdfData","");
|
||||
}
|
||||
LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||
zykInfo.setCreateBy(user.getUsername());
|
||||
|
@ -286,8 +288,8 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
|
|||
* @return 上传结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String,Object> uploadFileToSftp(Map<String,Object> downloadMap,String filePath){
|
||||
Map<String,Object> map = fileHandleUtil.uploadFileToSftp(downloadMap,filePath);
|
||||
public Map<String,String> uploadFileToSftp(String filePath){
|
||||
Map<String,String> map = fileHandleUtil.uploadFileToSftp(filePath);
|
||||
if(!map.get("code").equals("0")){
|
||||
return map;
|
||||
}
|
||||
|
|
|
@ -27,24 +27,24 @@ public class FileHandleUtil {
|
|||
@Value(value = "${jeecg.path.webapp}")
|
||||
private String downloadpath;
|
||||
|
||||
public Map<String,Object> uploadFile(ZykInfo zykInfo){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
public Map<String,String> uploadFile(ZykInfo zykInfo){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
Map<String,Object> directoryMap = getDirectory(zykInfo);
|
||||
Map<String,String> directoryMap = getDirectory(zykInfo);
|
||||
if(!directoryMap.get("code").equals("0")){
|
||||
return directoryMap;
|
||||
}
|
||||
//资源库文件路径
|
||||
String directory = directoryMap.get("directory").toString();
|
||||
String directory = directoryMap.get("directory");
|
||||
|
||||
if(!StringUtils.isEmpty(zykInfo.getFileName())) {
|
||||
//源文件路径
|
||||
Map<String,Object> fileNameMap = getFileName(zykInfo);
|
||||
Map<String,String> fileNameMap = getFileName(zykInfo);
|
||||
if(!fileNameMap.get("code").equals("0")){
|
||||
return fileNameMap;
|
||||
}
|
||||
//上传文件
|
||||
Map<String,Object> uploadMap = SFTPUtil.upload(null,sftpConfig,true,directory,fileNameMap.get("filePath").toString(),fileNameMap.get("fileName").toString());
|
||||
Map<String,String> uploadMap = SFTPUtil.upload(sftpConfig,true,directory,fileNameMap.get("filePath").toString(),fileNameMap.get("fileName").toString());
|
||||
if(!uploadMap.get("code").equals("0")){
|
||||
return uploadMap;
|
||||
}
|
||||
|
@ -53,36 +53,40 @@ public class FileHandleUtil {
|
|||
|
||||
if(!StringUtils.isEmpty(zykInfo.getPdfName())) {
|
||||
//源文件路径
|
||||
Map<String,Object> fileNameMap = getPdfName(zykInfo);
|
||||
Map<String,String> fileNameMap = getPdfName(zykInfo);
|
||||
if(!fileNameMap.get("code").equals("0")){
|
||||
return fileNameMap;
|
||||
}
|
||||
//上传文件
|
||||
Map<String,Object> uploadMap = SFTPUtil.upload(null,sftpConfig,true,directory,fileNameMap.get("filePath").toString(),fileNameMap.get("pdfName").toString());
|
||||
Map<String,String> uploadMap = SFTPUtil.upload(sftpConfig,true,directory,fileNameMap.get("filePath").toString(),fileNameMap.get("pdfName").toString());
|
||||
if(!uploadMap.get("code").equals("0")){
|
||||
return uploadMap;
|
||||
}
|
||||
map.put("pdfData",uploadMap.get("data"));
|
||||
}
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String,Object> uploadFileToSftp(Map<String,Object> sftpMap, String filePath){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
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,Object> fileNameMap = getFileName(filePath);
|
||||
Map<String,String> fileNameMap = getFileName(filePath);
|
||||
if(!fileNameMap.get("code").equals("0")){
|
||||
return fileNameMap;
|
||||
}
|
||||
//上传文件
|
||||
Map<String,Object> uploadMap = SFTPUtil.upload((ChannelSftp)sftpMap.get("sftp"),sftpConfig,false,"temp/",fileNameMap.get("filePath").toString(),fileNameMap.get("fileName").toString());
|
||||
Map<String,String> uploadMap = SFTPUtil.upload(sftpConfig,false,"temp/",fileNameMap.get("filePath").toString(),fileNameMap.get("fileName").toString());
|
||||
if(!uploadMap.get("code").equals("0")){
|
||||
return uploadMap;
|
||||
}
|
||||
map.put("data",uploadMap.get("data"));
|
||||
}
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
return map;
|
||||
}
|
||||
|
||||
|
@ -96,28 +100,33 @@ public class FileHandleUtil {
|
|||
return ss;
|
||||
}
|
||||
|
||||
public Map<String,Object> download(ChannelSftp sftp, String directory, String downloadFile){
|
||||
return SFTPUtil.download(sftp,sftpConfig,directory,downloadFile,getDownloadPath("temp"));
|
||||
public Map<String,String> download(String directory, String downloadFile){
|
||||
Map<String,String> map = SFTPUtil.download(sftpConfig,directory,downloadFile,getDownloadPath("temp"));
|
||||
// SFTPUtil.disChannel();
|
||||
// SFTPUtil.disSession();
|
||||
return map;
|
||||
}
|
||||
|
||||
public void downloadRes(String directory, String downloadFile,HttpServletResponse response) throws Exception{
|
||||
public void downloadRes(String downloadFile,HttpServletResponse response) throws Exception{
|
||||
SFTPUtil.writeFileToRes(sftpConfig,downloadFile,response);
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
}
|
||||
|
||||
public Map<String,Object> moveFile(ZykInfo zykInfo){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
public Map<String,String> moveFile(ZykInfo zykInfo){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
Map<String,Object> directoryMap = getDirectory(zykInfo);
|
||||
Map<String,String> directoryMap = getDirectory(zykInfo);
|
||||
if(!directoryMap.get("code").equals("0")){
|
||||
return directoryMap;
|
||||
}
|
||||
//资源库文件路径
|
||||
String directory = directoryMap.get("directory").toString();
|
||||
String directory = directoryMap.get("directory");
|
||||
|
||||
if(!StringUtils.isEmpty(zykInfo.getFileName())) {
|
||||
//源文件路径
|
||||
String newname = getName(zykInfo,false);
|
||||
Map<String,Object> uploadMap = SFTPUtil.moveFile(sftpConfig,zykInfo.getFileName(),directory,newname);
|
||||
Map<String,String> uploadMap = SFTPUtil.moveFile(sftpConfig,zykInfo.getFileName(),directory,newname);
|
||||
if(!uploadMap.get("code").equals("0")){
|
||||
return uploadMap;
|
||||
}
|
||||
|
@ -126,33 +135,30 @@ public class FileHandleUtil {
|
|||
|
||||
if(!StringUtils.isEmpty(zykInfo.getPdfName())) {
|
||||
String newname = getName(zykInfo,true);
|
||||
Map<String,Object> uploadMap = SFTPUtil.moveFile(sftpConfig,zykInfo.getFileName(),directory,newname);
|
||||
Map<String,String> uploadMap = SFTPUtil.moveFile(sftpConfig,zykInfo.getFileName(),directory,newname);
|
||||
if(!uploadMap.get("code").equals("0")){
|
||||
return uploadMap;
|
||||
}
|
||||
map.put("pdfData",uploadMap.get("data"));
|
||||
}
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<String,Object> moveFiles(Map<String,Object> sftpMap, ZykInfo zykInfo){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
public Map<String,String> moveFiles(ZykInfo zykInfo){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
Map<String,Object> directoryMap = getDirectory(zykInfo);
|
||||
Map<String,String> directoryMap = getDirectory(zykInfo);
|
||||
if(!directoryMap.get("code").equals("0")){
|
||||
return directoryMap;
|
||||
}
|
||||
//资源库文件路径
|
||||
String directory = directoryMap.get("directory").toString();
|
||||
String directory = directoryMap.get("directory");
|
||||
|
||||
if(!StringUtils.isEmpty(zykInfo.getFileName())) {
|
||||
List<String[]> list = getPathsAndNames(zykInfo,false);
|
||||
Map<String,Object> uploadMap = null;
|
||||
if(sftpMap!=null){
|
||||
uploadMap = SFTPUtil.moveFiles((ChannelSftp)sftpMap.get("sftp"),sftpConfig,directory,list);
|
||||
}else{
|
||||
uploadMap = SFTPUtil.moveFiles(sftpConfig,directory,list);
|
||||
}
|
||||
Map<String,String> uploadMap = SFTPUtil.moveFiles(sftpConfig,directory,list);
|
||||
if(!uploadMap.get("code").equals("0")){
|
||||
return uploadMap;
|
||||
}
|
||||
|
@ -160,20 +166,14 @@ public class FileHandleUtil {
|
|||
}
|
||||
if(!StringUtils.isEmpty(zykInfo.getPdfName())) {
|
||||
List<String[]> list = getPathsAndNames(zykInfo,true);
|
||||
Map<String,Object> uploadMap = null;
|
||||
if(sftpMap!=null){
|
||||
uploadMap = SFTPUtil.moveFiles((ChannelSftp)sftpMap.get("sftp"),sftpConfig,directory,list);
|
||||
}else{
|
||||
uploadMap = SFTPUtil.moveFiles(sftpConfig,directory,list);
|
||||
}
|
||||
Map<String,String> uploadMap = SFTPUtil.moveFiles(sftpConfig,directory,list);
|
||||
if(!uploadMap.get("code").equals("0")){
|
||||
return uploadMap;
|
||||
}
|
||||
map.put("pdfData",uploadMap.get("data"));
|
||||
}
|
||||
if(sftpMap!=null){
|
||||
SFTPUtil.disConnect((ChannelSftp)sftpMap.get("sftp"));
|
||||
}
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
return map;
|
||||
}
|
||||
|
||||
|
@ -181,13 +181,18 @@ public class FileHandleUtil {
|
|||
SFTPUtil.delete(sftpConfig,directory,downloadFile);
|
||||
}
|
||||
|
||||
public void disConnect(){
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源库文件路径
|
||||
* @param zykInfo
|
||||
* @return
|
||||
*/
|
||||
private Map<String,Object> getDirectory(ZykInfo zykInfo){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
private Map<String,String> getDirectory(ZykInfo zykInfo){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if(StringUtils.isEmpty(zykInfo.getXqxn())){
|
||||
|
@ -234,8 +239,8 @@ public class FileHandleUtil {
|
|||
* @param zykInfo
|
||||
* @return
|
||||
*/
|
||||
private Map<String,Object> getFileName(ZykInfo zykInfo){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
private Map<String,String> getFileName(ZykInfo zykInfo){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
String uploadFileName = getRealFileFullPath(zykInfo.getFileName());
|
||||
if(StringUtils.isEmpty(uploadFileName)){
|
||||
|
@ -290,8 +295,8 @@ public class FileHandleUtil {
|
|||
* @param fileNamePath
|
||||
* @return
|
||||
*/
|
||||
private Map<String,Object> getFileName(String fileNamePath){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
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)){
|
||||
|
@ -389,8 +394,8 @@ public class FileHandleUtil {
|
|||
* @param zykInfo
|
||||
* @return
|
||||
*/
|
||||
private Map<String,Object> getPdfName(ZykInfo zykInfo){
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
private Map<String,String> getPdfName(ZykInfo zykInfo){
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("code","0");
|
||||
String uploadFileName = getRealFileFullPath(zykInfo.getPdfName());
|
||||
if(StringUtils.isEmpty(uploadFileName)){
|
||||
|
|
|
@ -215,11 +215,16 @@ public class CommonController {
|
|||
private String uploadSftp(MultipartFile mf,String bizPath){
|
||||
try {
|
||||
Map<String,String> uploadMap = SFTPUtil.upload(sftpConfig,mf,bizPath);
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
if(uploadMap.get("code").equals("0")){
|
||||
return uploadMap.get("data");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}finally {
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
@ -463,7 +468,14 @@ public class CommonController {
|
|||
imgPath = imgPath.substring(0, imgPath.length() - 1);
|
||||
}
|
||||
if(CommonConstant.UPLOAD_TYPE_SFTP.equals(uploadType)) {
|
||||
SFTPUtil.writeFileToRes(sftpConfig,imgPath,response);
|
||||
try{
|
||||
SFTPUtil.writeFileToRes(sftpConfig,imgPath,response);
|
||||
}catch (Exception e){
|
||||
// e.printStackTrace();
|
||||
}finally {
|
||||
SFTPUtil.disChannel();
|
||||
SFTPUtil.disSession();
|
||||
}
|
||||
}else {
|
||||
String filePath = uploadpath + File.separator + imgPath;
|
||||
File file = new File(filePath);
|
||||
|
|
Loading…
Reference in New Issue