依赖
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency
示例
/** 接收文件
@ host: SFTP 服务器IP地址: 192.168.xx.xxx
@ port: S FTP 服务器端口: 22
@ userName: SFTP 服务器登录名称
@ password: SFTP 服务器登录密码
@ remotePath: SFTP 服务器文件目录: /home/ftpuser/
@ remoteFileName: SFTP 服务器文件名称: xxxx.txt
@ localFilePath: 本地文件目录: /data/
*/
public void download(String host, Integer port, String userName, String password, String remotePath, String remoteFileName, String localFilePath) {
FileOutputStream fieloutput = null;
Session sshSession = null;
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
sshSession = jsch.getSession(userName, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect(30000);
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
File file = new File(localFilePath+“/”+fieloutput);
fieloutput = new FileOutputStream(file);
sftp.cd(remotePath);
sftp.get(remotePath + remoteFileName, fieloutput);
} catch (Exception e) {
log.error("SFTP下载" + remotePath + remoteFileName + "文件异常:", e);
} finally {
if (null != fieloutput) {
try {
fieloutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
}
}
}
}
/** 发送文件
@ host: SFTP 服务器IP地址
@ port: S FTP 服务器端口
@ userName: SFTP 服务器登录名称
@ password: SFTP 服务器登录密码
@ remotePath: SFTP 服务器文件目录
@ remoteFileName: SFTP 服务器文件名称
@ localFilePath: 本地文件目录
*/
public static void upload(String host, int port, String userName, String password, String remoteFilePath, String remoteFileName, String localFilePath) throws EMPException {
FileInputStream in = null;
Session sshSession = null;
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(userName, host, port);
sshSession = jsch.getSession(userName, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
createDir(remoteFilePath, sftp);
File file = new File(localFilePath+“/”+fieloutput);
in = new FileInputStream(file);
sftp.put(in, remoteFileName);
} catch (Exception e) {
throw new EMPException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
}
}
}
}
/** 删除文件
@ host: SFTP 服务器IP地址
@ port: S FTP 服务器端口
@ userName: SFTP 服务器登录名称
@ password: SFTP 服务器登录密码
@ remotePath: SFTP 服务器文件目录
@ remoteFileName: SFTP 服务器文件名称
*/
public void delete(String host, Integer port, String userName, String password, String remotePath, String remoteFileName) {
FileOutputStream fieloutput = null;
Session sshSession = null;
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
sshSession = jsch.getSession(userName, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect(30000);
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
sftp.cd(remotePath);
sftp.rm(remotePath + remoteFileName);
} catch (Exception e) {
log.error("SFTP删除" + remotePath + remoteFileName + "文件异常:", e);
} finally {
if (null != fieloutput) {
try {
fieloutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
}
}
}
}
/**
创建SFTP目录
*/
private static void createDir(String createpath, ChannelSftp sftp) {
if (isDirExist(createpath, sftp)) {
sftp.cd(createpath);
return;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString(), sftp)) {
sftp.cd(filePath.toString());
} else {
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
}
}
}
/**
判断目录是否存在
*/
private static boolean isDirExist(String directory, ChannelSftp sftp) {
SftpATTRS sftpATTRS = sftp.lstat(directory);
return sftpATTRS.isDir();
}