提示:文章如有错误的地方请指出,以免误人子弟!
文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、导入maven jar包
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
二、上代码
- application.yml (根据自己业务来添加路径地址,用不上的可以删掉,我这里就不删了)
sftp:
# 服务器地址
host: 192.168.96.135
# 端口
port: 22
# 账号
userName: root
# 密码
password: wang521
# 图片的根路径
basePath: /home/nginx/image
# 音频的根路径
audioPath: /home/nginx/audio
# 视频的根路径
videoPath: /home/nginx/video
# channel连接超时时间
timeout: 30000
#连接次数
count: 10
#休眠时间
sleepTime: 6000
#服务器头像地址
titleImgsPath: http://192.168.96.135:80/image/
#服务器音频地址
titleAudiosPath: http://192.168.96.135:80/audio/
#服务器视频地址
titleVideosPath: http://192.168.96.135:80/video/
- 新建一个实体类,用起来方便的
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;
/**
* @author Mr.Tiger
* @date 2021/01/07 17:59
*/
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class SftpDTO implements Serializable {
/**
* description: Ip
*/
private String hostname;
/**
* description: 端口
*/
private Integer port;
/**
* description: 服务器用户名
*/
private String username;
/**
* description: 服务器密码
*/
private String password;
/**
* description: 连接超时
*/
private Integer timeout;
/**
* description: 服务器根路径
*/
private String remoteRootPath;
public SftpDTO() {
}
public SftpDTO(String hostname, Integer port, String username, String password, Integer timeout, String remoteRootPath) {
this.hostname = hostname;
this.port = port;
this.username = username;
this.password = password;
this.timeout = timeout;
this.remoteRootPath = remoteRootPath;
}
}
- sftp工具类
注意:
这里有个地方需要注意下,单个文件或多个文件上传只需要连接一次 sftp 就好了
import com.jcraft.jsch.*;
import com.wwwh.onlyone.common.entity.userInfo.dto.SftpDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* @author Mr.Tiger
* @date 2021/01/07 18:17
*/
@Slf4j
@Component
@RefreshScope
public class SftpUtil
{
/**
* description: count-> 连接超过次数,线程休眠
*/
@Value("${sftp.count}")
private long count;
/**
* description: sleepTime-> 休眠时间
*/
@Value("${sftp.sleepTime}")
private long sleepTime;
/**
* description: count1-> 已经连接次数
*/
private long count1 = 0;
/**
* description: 连接sftp服务器
*
* @param sftpConfig sftp实体类
* @return com.jcraft.jsch.ChannelSftp
* @author Mr.Tiger
*/
public ChannelSftp connect(SftpDTO sftpConfig) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(sftpConfig.getUsername(), sftpConfig.getHostname(), sftpConfig.getPort());
Session sshSession = jsch.getSession(sftpConfig.getUsername(), sftpConfig.getHostname(), sftpConfig.getPort());
log.info("Session created ... UserName=" + sftpConfig.getUsername() + ";host=" + sftpConfig.getHostname() + ";port=" + sftpConfig.getPort());
sshSession.setPassword(sftpConfig.getPassword());
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
log.info("Session connected ...");
//协议
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
log.info("登录成功");
} catch (Exception e) {
try {
count1 += 1;
if (count == count1) {
throw new RuntimeException(e);
}
Thread