Java 操作sftp工具类
/**
* Project Name: pps-deferred-server
* File Name: SshUtil.java
* @Date: 2021年3月10日 上午9:36:21
* @author: zhaozm
* @since: JDK 1.8
* Copyright (c) 2021, travelsky All Rights Reserved.
*
*/
package com.acca.opra.pps.deferred.data.imports.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
/**
* @ClassName: SshUtil
* @Description: 远程命令/sftp工具类
* @author: zhaozm
* @date: 2021年3月10日 上午9:36:21
* @version: 1.0.0
*/
@Slf4j
public class SshUtil {
public static final String SFTP = "sftp";
private static final String SHELL = "exec";
/**
* 执行远程命令
*
* @param ip
* @param user
* @param psw
* @throws Exception
*/
public static String execCmd(String ip, String user, String psw, String cmd) throws Exception {
//连接服务器,采用默认端口
JSch jsch = new JSch();
Session session = jsch.getSession(user, ip);
Channel channel = connect(session, psw, SHELL);
String result = null;
try {
ChannelExec channelExec = (ChannelExec) channel;
//获取输入流和输出流
InputStream in = channel.getInputStream();
channelExec.setCommand(cmd);
channelExec.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
result = new String(tmp, 0, i);
}
if (channel.isClosed()) {
break;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
result = e.toString();
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
session.disconnect();
channel.disconnect();
}
return result;
}
/**
* 执行sftp传输
*
* @param ip
* @param user
* @param psw
* @param localFile 本地文件
* @param dstDir
* @throws Exception
*/
public static void sftp(String ip, String user, String psw, File localFile, String dstDir) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(user, ip);
//连接服务器,采用默认端口
Channel channel = connect(session, psw, SFTP);
try {
ChannelSftp sftp = (ChannelSftp) channel;
sftp.connect();
//本地文件
if (!localFile.isFile())
return;
InputStream in = new FileInputStream(localFile);
// 目的文件
OutputStream out = sftp.put(dstDir + File.separator + localFile.getName());
byte b[] = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
out.flush();
out.close();
in.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
session.disconnect();
channel.disconnect();
}
}
/**
* 执行sftp传输
*
* @param ip
* @param user
* @param psw
* @param dstDir 目标文件夹
* @param destName 目标文件名
* @throws Exception
*/
public static void sftp(String ip, String user, String psw, InputStream in, String dstDir, String destName) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(user, ip);
//连接服务器,采用默认端口
Channel channel = connect(session, psw, SFTP);
try {
ChannelSftp sftp = (ChannelSftp) channel;
sftp.connect();
// 目的文件
OutputStream out = sftp.put(dstDir + "/" + destName);
byte b[] = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
out.flush();
out.close();
in.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
session.disconnect();
channel.disconnect();
}
}
/**
* 列出目录下的文件
*
* @param directory 目录
* @param sftp
* @return
* @throws SftpException
*/
public static Vector<?> listFiles(String ip, String user, String psw,String directory) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(user, ip);
//连接服务器,采用默认端口
Channel channel = connect(session, psw, SFTP);
Vector<?> v = null;
try {
ChannelSftp sftp = (ChannelSftp) channel;
sftp.connect();
v = sftp.ls(directory);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
session.disconnect();
channel.disconnect();
}
return v;
}
/**
* 文件删除
*
* @param directory 目录
* @param deleteFile 要删除的文件名
* @param sftp
*/
public static void delete(String ip, String user, String psw,String directory, String deleteFile) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(user, ip);
//连接服务器,采用默认端口
Channel channel = connect(session, psw, SFTP);
try {
ChannelSftp sftp = (ChannelSftp) channel;
sftp.connect();
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
session.disconnect();
channel.disconnect();
}
}
/**
*
* delete: 清空目录下文件
* @author: zhaozm
* @Date: 2021年3月15日 下午4:34:57
* @param ip
* @param user
* @param psw
* @param directory
* @throws Exception
*/
public static void delete(String ip, String user, String psw,String directory) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(user, ip);
//连接服务器,采用默认端口
Channel channel = connect(session, psw, SFTP);
try {
ChannelSftp sftp = (ChannelSftp) channel;
sftp.connect();
sftp.cd(directory);
Vector<?> v = sftp.ls(directory);
Iterator<?> iterator = v.iterator();
while (iterator.hasNext()) {
ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) iterator.next();
if(!Pattern.matches("^[.]*", file.getFilename())) {
sftp.rm(file.getFilename());
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
session.disconnect();
channel.disconnect();
}
}
/**
* 下载文件
* @param directory 下载目录
* @param downloadFile 下载的文件名
* @return 字节数组
* @throws Exception
*/
public static InputStream download(String ip, String user, String psw, String directory, String fileName) throws Exception {
if (StringUtils.isBlank(directory))
{
return null;
}
JSch jsch = new JSch();
Session session = jsch.getSession(user, ip);
//连接服务器,采用默认端口
Channel channel = connect(session, psw, SFTP);
InputStream is = null;
try {
ChannelSftp sftp = (ChannelSftp) channel;
sftp.connect();
sftp.cd(directory);
is = sftp.get(fileName);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
session.disconnect();
channel.disconnect();
}
return is;
}
/**
* 连接服务器
*
* @param session
* @param psw
* @param type
* @return
* @throws Exception
*/
public static Channel connect(Session session, String psw, String type) throws Exception {
//如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("session is null");
}
//设置登陆主机的密码
session.setPassword(psw);//设置密码
//设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking", "no");
//设置登陆超时时间
session.connect(30000);
//创建通信通道
return session.openChannel(type);
}
}
pom.xml