package cn.com.soulfox.jsch;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.*;
/**
* @author xxxx
* @create 2024/7/4 14:28
*/
@Component
@Slf4j
public class JschSftpUtil {
private static JschSftpConneciton jschSftpConneciton;
@Autowired
public void setJschSftpConneciton(JschSftpConneciton jschSftpConneciton) {
JschSftpUtil.jschSftpConneciton = jschSftpConneciton;
}
/**
* 下载文件
* @param remotePath 远程目录
* @param downloadFileName 待下载的远程文件名称
* @param localSavePath 下载文件保存的本地目录
*/
public static void downloadFile(String remotePath, String downloadFileName, String localSavePath) {
SftpWrapper sftpWrapper = null;
try {
//sftp连接对象
sftpWrapper = jschSftpConneciton.connectWithRetry();
//进入远程服务器指定的目录
sftpWrapper.getSftp().cd(remotePath);
if (!checkLocalPath(localSavePath)) {
log.info("本地目录[{}]不存在,且新建失败+++++", localSavePath);
return;
}
String localFile = checkPathEnd(localSavePath) + downloadFileName;
File outFile = new File(localFile);
sftpWrapper.getSftp().get(downloadFileName, new FileOutputStream(outFile));
log.info("从远程目录[{}]下载文件[{}]到本地目录[{}]成功", remotePath, downloadFileName, localSavePath);
} catch (SftpException e) {
log.info("从远程目录[{}]下载文件[{}]到本地目录[{}]失败", remotePath, downloadFileName, localSavePath);
log.error("下载文件失败: ", e);
} catch (Exception e) {
log.info("从远程目录[{}]下载文件[{}]到本地目录[{}]失败", remotePath, downloadFileName, localSavePath);
log.error("下载文件失败: ", e);
} finally {
if (sftpWrapper != null) {
sftpWrapper.disconnect();
}
}
}
/**
*
* @param localDir 保存上传文件的本地目录
* @param uploadFileName 上传文件名称
* @param remoteSaveDir 保存上传文件的远程目录, 建议使用绝对路径
* 如果使用相对路径,建议基准目录使用sfpt登录后所在的目录
* 这个目录,使用channelSftp.goHome()可以获取
*/
public static void uploadFile(String localDir, String uploadFileName, String remoteSaveDir) {
String uploadFilePath = checkPathEnd(localDir) + uploadFileName;
File uploadFile = new File(uploadFilePath);
uploadFile(uploadFile, remoteSaveDir);
}
/**
* 上传文件
* @param uploadFilePath 本地文件的路径
* @param remoteSaveDir 保存上传文件的远程目录, 建议使用绝对路径
* 如果使用相对路径,建议基准目录使用sfpt登录后所在的目录
* 这个目录,使用channelSftp.goHome()可以获取
*/
public static void uploadFile(String uploadFilePath, String remoteSaveDir) {
File uploadFile = new File(uploadFilePath);
uploadFile(uploadFile, remoteSaveDir);
}
/**
* 上传文件
* @param uploadFile 上传文件的File对象
* @param remoteSavePath 保存上传文件的远程目录, 建议使用绝对路径
* 如果使用相对路径,建议基准目录使用sfpt登录后所在的目录
* 这个目录,使用channelSftp.goHome()可以获取
*/
public static void uploadFile(File uploadFile, String remoteSavePath) {
if(uploadFile == null ){
log.info("本地文件对象不存在++++");
return;
}
if(!uploadFile.exists()){
log.info("本地文件[{}]不存在", uploadFile.getAbsoluteFile());
return;
}
InputStream is = null;
try {
is = new FileInputStream(uploadFile);
} catch (FileNotFoundException e) {
log.info("获取本地文件[{}]的文件流失败", uploadFile.getAbsoluteFile());
log.error("获取文件流失败: ", e);
if(is != null){
try {
is.close();
} catch (IOException ioException) {
}
}
return;
}
SftpWrapper sftpWrapper = null;
try {
//sftp连接对象
sftpWrapper = jschSftpConneciton.connectWithRetry();
//检查远程目录,不存在则创建
createLinuxRemoteDirs(sftpWrapper.getSftp(), remoteSavePath);
//进入用户home目录,
sftpWrapper.getSftp().cd(sftpWrapper.getSftp().getHome());
//保证当前目录在上传文件保存的目录
sftpWrapper.getSftp().cd(remoteSavePath);
//上传文件
sftpWrapper.getSftp().put(is, uploadFile.getName());
} catch (SftpException e) {
log.info("上传本地文件[{}]到远程目录[{}]失败", uploadFile.getAbsoluteFile(), remoteSavePath);
log.error("上传本地文件失败: ", e);
} catch (Exception e) {
log.info("上传本地文件[{}]到远程目录[{}]失败", uploadFile.getAbsoluteFile(), remoteSavePath);
log.error("上传本地文件失败: ", e);
} finally {
if (sftpWrapper != null) {
sftpWrapper.disconnect();
}
if(is != null){
try {
is.close();
} catch (IOException e) {
}
}
}
}
/**
* 检查目录结是否以目录分隔符结尾
* 如果不是,则加上目录分隔符结尾
*
* @param localPath 本地目录
* @return
*/
private static String checkPathEnd(String localPath) {
if (localPath.endsWith("/") || localPath.endsWith("\\")) {
return localPath;
}
return localPath + File.separator;
}
/**
* 检查本地目录是否存在,不存在就创建。
* 为了防止其他线程已创建目录,加上了重试代码
*
* @param localPath 本地目录
* @return
*/
private static boolean checkLocalPath(String localPath) {
int maxRetryCount = 5;
int retry = 0;
do {
File localDir = new File(localPath);
if (localDir.exists()) {
return true;
}
boolean result = localDir.mkdirs();
if (result) {
return true;
}
try {
Thread.sleep(1000L);//休息1秒
} catch (InterruptedException e) {
}
} while (retry++ < maxRetryCount);
return false;
}
/**
* 检查和创建[ linux系统 ]的远程多级目录,
* 外部调用, 单纯的创建远程目录,不作其他操作
* @param remoteDir 远程目录
* @throws SftpException
*/
public static void createLinuxRemoteDirs( String remoteDir) throws SftpException {
SftpWrapper sftpWrapper = null;
try {
sftpWrapper = jschSftpConneciton.connectWithRetry();
createLinuxRemoteDirs(sftpWrapper.getSftp(), remoteDir);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 23 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/5e7ad802171b40a5ad9349057134e374_athjyh.jpg!1)
Winson.J
- 粉丝: 135
- 资源: 2
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)