- 添加依赖, 用后者而非前者,前者可能报错
<dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.55</version></dependency><dependency><groupId>com.github.mwiede</groupId><artifactId>jsch</artifactId><version>0.2.24</version></dependency>
- 工具类代码
import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Properties;public class SFTPUtils {private static final Logger LOG = LoggerFactory.getLogger(SFTPUtils.class);public static boolean uploadFile(String host, int port, String username, String password,String localPath, String remoteDir) {LOG.info(">>>>开始执行ftp上传文件,host={},port={},username={},password={},localPath={},remoteDir={}", host,port,username, password,localPath,remoteDir);ChannelSftp sftp = null;Session session = null;FileInputStream fis = null;try {JSch jsch = new JSch();session = jsch.getSession(username, host, port);session.setPassword(password);Properties config = new Properties();config.put("StrictHostKeyChecking", "no"); session.setConfig(config);session.connect();Channel channel = session.openChannel("sftp");channel.connect();sftp = (ChannelSftp) channel;try {sftp.cd(remoteDir);} catch (SftpException e) {mkdirs(sftp, remoteDir);}File file = new File(localPath);fis = new FileInputStream(file);sftp.put(fis, file.getName());LOG.info("<<<结束执行ftp上传文件,上传成功,host={},port={},username={},password={},localPath={},remoteDir={}", host,port,username, password,localPath,remoteDir);return true;} catch (Exception e) {LOG.error("<<<结束执行ftp上传文件,上传失败,host={},port={},username={},password={},localPath={},remoteDir={}", host,port,username, password,localPath,remoteDir, e);return false;} finally {try {if (fis != null) fis.close();if (sftp != null) sftp.exit();if (session != null) session.disconnect();} catch (Exception e) {LOG.error("关闭ftp资源发生异常,host={},port={},username={},password={},localPath={},remoteDir={}", host,port,username, password,localPath,remoteDir, e);}}}private static void mkdirs(ChannelSftp sftp, String path) throws SftpException {String[] folders = path.split("/");for (String folder : folders) {if (folder.isEmpty()) continue;try {sftp.cd(folder);} catch (SftpException e) {sftp.mkdir(folder);sftp.cd(folder);}}}public static int downloadFiles(String host, int port, String username, String password,String localDirectory, List<String> remoteFilePaths) {String remoteFilePathsStr = String.join(",", remoteFilePaths);LOG.info(">>>>开始执行ftp文件下载,host={},port={},username={},password={},localDirectory={},remoteFilePaths={}", host,port,username, password,localDirectory, remoteFilePathsStr);ChannelSftp sftp = null;Session session = null;int totalNum = remoteFilePaths.size();int count = 0;try {JSch jsch = new JSch();session = jsch.getSession(username, host, port);session.setPassword(password);Properties config = new Properties();config.put("StrictHostKeyChecking", "no"); session.setConfig(config);session.connect();sftp = (ChannelSftp) session.openChannel("sftp");sftp.connect();File localDirectoryFile = new File(localDirectory);if (!localDirectoryFile.exists()) {localDirectoryFile.mkdirs();}for (String remotePath : remoteFilePaths) {String fileName = getFileNameFromPath(remotePath);String localPath = localDirectory + File.separator + fileName;sftp.get(remotePath, localPath);count++;LOG.info("---执行ftp文件下载成功,host={},port={},remotePath={},localPath={},当前下载成功数目={}, 待下载数目={}", host,port, remotePath, localPath,count, totalNum-count);}}catch (Exception e){LOG.error("<<<<执行ftp文件下载过程中发生异常,host={},port={},username={},password={},localDirectory={},remoteFilePaths={}, 下载成功数目={}", host,port,username, password,localDirectory, remoteFilePathsStr, count, e);}finally {try {if (sftp != null){sftp.exit();}if (session != null){session.disconnect();}} catch (Exception e) {LOG.error("关闭ftp资源发生异常,host={},port={},username={},password={},localDirectory={},remoteFilePaths={}, 下载成功数目={}", host,port,username, password,localDirectory, remoteFilePathsStr, count, e);}}return count;}private static String getFileNameFromPath(String path) {int lastSeparator = path.lastIndexOf('/');if (lastSeparator >= 0) {return path.substring(lastSeparator + 1);}return path;}
}