1、引入jar包
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
2、连接下载
package com.example.mybatis_service.ftp;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
/**
* @Description TODO
*/
@Slf4j
public class SSHRemoteCall {
// 私有的对象
private static SSHRemoteCall sshRemoteCall;
/**
* 私有的构造方法
*/
private SSHRemoteCall() {
}
// 懒汉式,线程不安全,适合单线程
public static SSHRemoteCall getInstance() {
if (sshRemoteCall == null) {
sshRemoteCall = new SSHRemoteCall();
}
return sshRemoteCall;
}
// 懒汉式,线程安全,适合多线程
public static synchronized SSHRemoteCall getInstance2() {
if (sshRemoteCall == null) {
sshRemoteCall = new SSHRemoteCall();
}
return sshRemoteCall;
}
private static final int DEFAULT_PORT = 22;// 默认端口号
private int port;// 端口号
private static final String userName = "555";// 账号
private static final String password44 = "555555";// 密码
private static ChannelSftp sftp = null;
private static Channel channel = null;
private static Session sshSession = null;
private static List<String> listFileNames(Channel channel, String dir) {
List<String> list = new ArrayList<String>();
ChannelSftp sftp = null;
try {
sftp = (ChannelSftp) channel;
Vector vector = sftp.ls(dir);//获取文件列表
for (Object item : vector) {
if (item instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) item).getFilename();
if (fileName.equals(".") || fileName.equals("..")) {
continue;
}
System.out.println(fileName);
list.add(fileName);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private static Channel getChannel(String host, int port, String username, final String password) {
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();
//LOG.debug("Session connected!");
channel = sshSession.openChannel("sftp");
channel.connect();
// LOG.debug("Channel connected!");
} catch (Exception e) {
e.printStackTrace();
}
return channel;
}
/**
* 下载
* @param channel
* @param src
* @param localPath
*/
private static void getFileFromSftpServer(Channel channel, String src, String localPath, String fileNameTo) {
ChannelSftp sftp = null;
if (StringUtils.isBlank(fileNameTo)){
log.error("要下载的文件名称为空,上传失败");
return;
}
try {
sftp = (ChannelSftp) channel;
Vector vector = sftp.ls(src);//获取文件目录下所有的文件
for (Object item : vector) {
if (item instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) item).getFilename();
if (fileName.equals(".") || fileName.equals("..")) {
continue;
}
if (!fileNameTo.equals(fileName)){
continue;
}
System.out.println(fileName);
File localpath = new File(localPath);
File[] fileList = localpath.listFiles();
if (!localpath.exists()) {
localpath.mkdirs();
localPath = localpath.getPath();
}
for (File file : fileList) {
if (fileName.equals(file.getName())) {
file.delete();
}
}
System.out.println(localPath);
String localFileName = localPath + File.separator + fileName;
sftp.get(src + "/" + fileName, localPath);
}
}
} catch (Exception e) {
log.error("文件上传失败!");
e.printStackTrace();
}
}
private static void putFileToSftpServer(Channel channel, String src, String localPath) {
List<String> list = new ArrayList<String>();
try {
sftp = (ChannelSftp) channel;
File file = new File(localPath);
if (!file.exists()) {
log.error("路径错误:{}", localPath);
return;
}
File[] files = file.listFiles();
for (File item : files) {
list.add(item.getName());
System.out.println("getPath:" + item.getPath());
sftp.put(localPath + File.separator + item.getName(), src);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void closeResource(Channel channel, ChannelSftp sftp, Session session) {
if (channel != null) {
if (channel.isConnected()) {
channel.disconnect();
}
}
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
public static void main(String[] args) {
// 连接到指定的服务器
try {
// ip地址
String ipAddress = "192.168.1.88";
channel = getChannel(ipAddress, 22, userName, password);
String src = "/home/123/";
String dst = "E:\\123";
String fileName = "123.jpg";
List<String> str = listFileNames(channel, src);
System.out.println("目录下包含的文件名称为:" + str);
for (String a : str) {
System.out.println("文件名为:" + a);
}
getFileFromSftpServer(channel, src, dst, fileName);
closeResource(channel, sftp, sshSession);
} catch (Exception e) {
// 打印错误信息
log.error("远程连接失败......");
e.printStackTrace();
}
}
}