1、pom.xml
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
2、工具方法示例。
/**
*
* 密钥文件连接
*
* @param sftpTimeOut 连接超时时间
* @param sftpUserName 登录服务的用户名
* @param sftpIp 登录服务的IP
* @param port 端口号 如: 22
* @param priKeyFile 密钥文件地址(注意是地址)--ppk文件存放地址
* @param passphrase 密码--可以为空
* @param downloadFile 下载地址
* @param saveFile 保存的地址
* @param fileNames 下载的文件名
* @return 返回值是按业务要求是否需要
*/
public static String priKeyConnect(String sftpIp, String sftpUserName, int sftpTimeOut, int port, String priKeyFile,
String passphrase, String downloadFile, String saveFile, List<String> fileNames,
Map<String, String> downLoadFilePaths) {
XxlJobLogger.log("从sftp下载前的准备数据,端口【" + port + "】IP【" + sftpIp + "】秘钥文件【" + priKeyFile + "】下载地址【" + downloadFile
+ "】保存地址【" + saveFile + "】文件名称【" + JSONObject.toJSONString(fileNames) + "】");
boolean result = false;
ChannelSftp sftp = null;
Session session = null;
Channel channel = null;
JSch jsch = new JSch