SFTP上传,
This commit is contained in:
parent
5830dcbb83
commit
e1f5207804
|
@ -1,7 +1,6 @@
|
|||
package org.jeecg.common.util;
|
||||
|
||||
import com.jcraft.jsch.*;
|
||||
import org.apache.commons.compress.utils.Lists;
|
||||
import org.jeecg.common.constant.SymbolConstant;
|
||||
import org.jeecg.common.util.text.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -116,13 +115,15 @@ public class SFTPUtil {
|
|||
* @param uploadFileName 要上传的文件名称(重新定义的文件名称)
|
||||
* @param
|
||||
*/
|
||||
public static Map<String,String> upload(SftpConfig sftpConfig,boolean isConcat, String directory, String uploadFilePath, String uploadFileName) {
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
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>();
|
||||
map.put("code","0");
|
||||
map.put("msg","上传成功");
|
||||
ChannelSftp sftp = null;
|
||||
// ChannelSftp sftp = null;
|
||||
try{
|
||||
sftp = connect(sftpConfig);
|
||||
if(sftp == null){
|
||||
sftp = connect(sftpConfig);
|
||||
}
|
||||
try {
|
||||
if(isConcat){
|
||||
directory = getDirectory(sftpConfig.getUploadpath(),directory);
|
||||
|
@ -156,9 +157,9 @@ public class SFTPUtil {
|
|||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
if(sftp!=null){
|
||||
disConnect(sftp);
|
||||
}
|
||||
// if(sftp!=null){
|
||||
// disConnect(sftp);
|
||||
// }
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
@ -222,13 +223,16 @@ public class SFTPUtil {
|
|||
* @param downloadFile 下载的文件
|
||||
* @param saveFile 存在本地的路径
|
||||
*/
|
||||
public static Map<String,String> download(SftpConfig sftpConfig, String directory, String downloadFile, String saveFile) {
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
public static Map<String,Object> download(ChannelSftp sftp,SftpConfig sftpConfig, String directory, String downloadFile, String saveFile) {
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
map.put("code","0");
|
||||
map.put("msg","删除成功");
|
||||
ChannelSftp sftp = null;
|
||||
// ChannelSftp sftp = null;
|
||||
try{
|
||||
sftp = connect(sftpConfig);
|
||||
if(sftp == null){
|
||||
sftp = connect(sftpConfig);
|
||||
map.put("sftp",sftp);
|
||||
}
|
||||
OutputStream output = null;
|
||||
try {
|
||||
File localDirFile = new File(saveFile);
|
||||
|
@ -263,9 +267,9 @@ public class SFTPUtil {
|
|||
map.put("code","1");
|
||||
map.put("msg","sftp异常:" + e.getMessage());
|
||||
}finally {
|
||||
if(sftp!=null){
|
||||
disConnect(sftp);
|
||||
}
|
||||
// if(sftp!=null){
|
||||
// disConnect(sftp);
|
||||
// }
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
@ -373,8 +377,8 @@ public class SFTPUtil {
|
|||
* @param newname 目的文件名称
|
||||
* @param
|
||||
*/
|
||||
public static Map<String,String> moveFile(SftpConfig sftpConfig, String oldpath, String directory, String newname) {
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
public static Map<String,Object> moveFile(SftpConfig sftpConfig, String oldpath, String directory, String newname) {
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
map.put("code","0");
|
||||
map.put("msg","上传成功");
|
||||
ChannelSftp sftp = null;
|
||||
|
@ -446,8 +450,8 @@ public class SFTPUtil {
|
|||
return map;
|
||||
}
|
||||
|
||||
public static Map<String,String> moveFiles(SftpConfig sftpConfig, String directory, List<String[]> list) {
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
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;
|
||||
|
@ -526,6 +530,85 @@ public class SFTPUtil {
|
|||
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>();
|
||||
map.put("code","0");
|
||||
map.put("msg","上传成功");
|
||||
try {
|
||||
if(sftp == null){
|
||||
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 {
|
||||
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param directory 要删除文件所在目录
|
||||
|
|
Loading…
Reference in New Issue