SFTP多文件上传,删除

时间:2023-03-09 13:10:36
SFTP多文件上传,删除

  公司项目中需要把项目的相关文件上传到服务器的tomcat中,需要在项目中进行以下几步操作:

  1.添加项目信息,包括名称,描述,服务器ip,sftp的用户名,密码,端口号等,存在配置,部署,删除等操作

  2.配置:显示出文件信息,包括文件路径,目标路径,类型(上传,删除),状态(是否部署),

  3.点击部署时进行自动的部署,可以是文件上传,也可以是相关文件的删除

结合网上有关sftp完成的sftp工具类,只使用了多文件上传和文件删除功能,其他没有进行测试,多文件上传需要本地路径和远程路径参数,文件夹删除需要远程路径参数

 package MyUtils;

 import com.jcraft.jsch.*;
import org.apache.log4j.Logger; import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector; /**
* sftp工具类
* */
public class SFTPUtils {
private static Logger log = Logger.getLogger(SFTPUtils.class.getName());
private static ChannelSftp sftp = null;
private static Session sshSession = null;
private static Integer i = 0; /**
* 通过SFTP连接服务器
*/
public static void connect(String ip, String username, String password, Integer port) throws Exception { JSch jsch = new JSch();
try {
if (port <= 0) {
// 连接服务器,采用默认端口
sshSession = jsch.getSession(username, ip);
} else {
// 采用指定的端口连接服务器
sshSession = jsch.getSession(username, ip, port);
} // 如果服务器连接不上,则抛出异常
if (sshSession == null) {
throw new Exception("服务器异常");
} // 设置登陆主机的密码
sshSession.setPassword(password);// 设置密码
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
sshSession.setConfig("StrictHostKeyChecking", "no");
// 设置登陆超时时间
sshSession.connect(300000);
Channel channel = sshSession.openChannel("sftp");
channel.connect(); sftp = (ChannelSftp) channel; } catch (Exception e) {
e.printStackTrace();
throw e;
}
} /**
* 关闭连接
*/
public static void disconnect() {
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
if (log.isInfoEnabled()) {
log.info("已关闭sftp");
}
}
}
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
if (log.isInfoEnabled()) {
log.info("已关闭sshSession");
}
}
}
} /**
* 批量下载文件
*
* @param remotePath:远程下载目录(以路径符号结束)
*
* @param localPath:本地保存目录(以路径符号结束,D:\Duansha\sftp\)
*
* @param fileFormat:下载文件格式(以特定字符开头,为空不做检验)
*
* @param fileEndFormat:下载文件格式(文件格式)
* @param del:下载后是否删除sftp文件
* @return
*/
public List<String> batchDownLoadFile(String remotePath, String localPath,
String fileFormat, String fileEndFormat, boolean del) throws SftpException {
List<String> filenames = new ArrayList<String>(); Vector v = sftp.ls(remotePath);
if (v.size() > 0) {
System.out.println("本次处理文件个数不为零,开始下载...fileSize=" + v.size());
Iterator it = v.iterator();
while (it.hasNext()) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) it.next();
String filename = entry.getFilename();
SftpATTRS attrs = entry.getAttrs();
if (!attrs.isDir()) {
boolean flag = false;
String localFileName = localPath + filename;
fileFormat = fileFormat == null ? "" : fileFormat
.trim();
fileEndFormat = fileEndFormat == null ? ""
: fileEndFormat.trim();
// 三种情况
if (fileFormat.length() > 0 && fileEndFormat.length() > 0) {
if (filename.startsWith(fileFormat) && filename.endsWith(fileEndFormat)) {
flag = downloadFile(remotePath, filename, localPath, filename);
if (flag) {
filenames.add(localFileName);
if (flag && del) {
deleteSFTP(remotePath);
}
}
}
} else if (fileFormat.length() > 0 && "".equals(fileEndFormat)) {
if (filename.startsWith(fileFormat)) {
flag = downloadFile(remotePath, filename, localPath, filename);
if (flag) {
filenames.add(localFileName);
if (flag && del) {
deleteSFTP(remotePath);
}
}
}
} else if (fileEndFormat.length() > 0 && "".equals(fileFormat)) {
if (filename.endsWith(fileEndFormat)) {
flag = downloadFile(remotePath, filename, localPath, filename);
if (flag) {
filenames.add(localFileName);
if (flag && del) {
deleteSFTP(remotePath);
}
}
}
} else {
flag = downloadFile(remotePath, filename, localPath, filename);
if (flag) {
filenames.add(localFileName);
if (flag && del) {
deleteSFTP(remotePath);
}
}
}
}
}
}
if (log.isInfoEnabled()) {
log.info("download file is success:remotePath=" + remotePath
+ "and localPath=" + localPath + ",file size is"
+ v.size());
} return filenames;
} /**
* 下载单个文件
*
* @param remotePath:远程下载目录(以路径符号结束)e
* @param remoteFileName:下载文件名
* @param localPath:本地保存目录(以路径符号结束)
* @param localFileName:保存文件名
* @return
*/
public boolean downloadFile(String remotePath, String remoteFileName, String localPath, String localFileName) {
FileOutputStream fieloutput = null;
try {
// sftp.cd(remotePath);
File file = new File(localPath + localFileName);
// mkdirs(localPath + localFileName);
fieloutput = new FileOutputStream(file);
sftp.get(remotePath + remoteFileName, fieloutput);
if (log.isInfoEnabled()) {
log.info("===DownloadFile:" + remoteFileName + " success from sftp.");
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} finally {
if (null != fieloutput) {
try {
fieloutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
} /**
* 上传单个文件
*
* @param remotePath:远程保存目录
* @param localPath:本地上传目录(以路径符号结束)
* @return
*/
public static void uploadFile(String remotePath, String localPath) throws Exception {
FileInputStream in = null;
File localFile = new File(localPath);
sftp.cd(remotePath);
in = new FileInputStream(localFile);
sftp.put(in, localFile.getName()); if (in != null) {
in.close();
}
} /**
* 批量上传文件
*
* @param remotePath:远程保存目录
* @param localPath:本地上传目录(以路径符号结束)
* @return
*/
public static boolean bacthUploadFile(String remotePath, String localPath) throws Exception {
File localFile = new File(localPath);
boolean flag = true;
//进入远程路径
try {
if (!isDirExist(remotePath)) {
sftp.mkdir(remotePath);
sftp.cd(remotePath);
} else {
sftp.cd(remotePath);
}
//本地文件上传
File file = new File(localPath);
//本地文件上传方法
copyFile(file, sftp.pwd()); } catch (Exception e) {
e.printStackTrace();
flag = false;
throw e;
} return flag;
} private static void copyFile(File file, String pwd) throws Exception {
if (file.isDirectory()) {
File[] list = file.listFiles();
String fileName = file.getName();
sftp.cd(pwd);
System.out.println("正在创建目录:" + sftp.pwd() + "/" + fileName);
sftp.mkdir(fileName);
System.out.println("目录创建成功:" + sftp.pwd() + "/" + fileName);
//远程路径发生改变
pwd = pwd + "/" + file.getName();
sftp.cd(file.getName()); for (int i = 0; i < list.length; i++) {
copyFile(list[i], pwd);
}
} else {
//不是目录,直接进入改变后的远程路径,进行上传
sftp.cd(pwd); System.out.println("正在复制文件:" + file.getAbsolutePath());
InputStream instream = null;
OutputStream outstream = null;
outstream = sftp.put(file.getName());
instream = new FileInputStream(file); byte b[] = new byte[1024];
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
} outstream.flush();
outstream.close();
instream.close(); } } /**
* 删除本地文件
*
* @param filePath
* @return
*/
public boolean deleteFile(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return false;
} if (!file.isFile()) {
return false;
}
boolean rs = file.delete();
if (rs && log.isInfoEnabled()) {
log.info("delete file success from local.");
}
return rs;
} /**
* 创建目录
*
* @param createpath
* @return
*/
public static void createDir(String createpath) {
try {
if (isDirExist(createpath)) {
sftp.cd(createpath);
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString())) {
sftp.cd(filePath.toString());
} else {
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
} }
} catch (SftpException e) {
e.printStackTrace();
}
} /**
* 判断目录是否存在
*
* @param directory
* @return
*/
public static boolean isDirExist(String directory) {
try {
Vector<?> vector = sftp.ls(directory);
if (null == vector) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
} /**
* 删除stfp文件
*
* @param directory:要删除文件所在目录
*/
public static void deleteSFTP(String directory) {
try {
if (isDirExist(directory)) {
Vector<ChannelSftp.LsEntry> vector = sftp.ls(directory);
if (vector.size() == 1) { // 文件,直接删除
sftp.rm(directory);
} else if (vector.size() == 2) { // 空文件夹,直接删除
sftp.rmdir(directory);
} else {
String fileName = "";
// 删除文件夹下所有文件
for (ChannelSftp.LsEntry en : vector) {
fileName = en.getFilename();
if (".".equals(fileName) || "..".equals(fileName)) {
continue;
} else {
deleteSFTP(directory + "/" + fileName);
}
}
// 删除文件夹
sftp.rmdir(directory);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 如果目录不存在就创建目录
*
* @param path
*/
public void mkdirs(String path) {
File f = new File(path); String fs = f.getParent(); f = new File(fs); if (!f.exists()) {
f.mkdirs();
}
} /**
* 测试
*/
public static void main(String[] args) {
// 本地存放地址
String localPath = "F:\\java\\src\\main\\webapp\\resources";
// Sftp下载路径
String sftpPath = "/home/sftp/";
List<String> filePathList = new ArrayList<String>();
try {
connect("111.16.123.12", "ftptest1", "123456", 22);
//deleteSFTP(sftpPath);
// 上传
//bacthUploadFile(sftpPath, localPath);
//deleteSFTP("/home/ftp/ff");
} catch (Exception e) {
e.printStackTrace();
} finally {
sftp.disconnect();
}
}
}