1.引入相关依赖

1
2
3
4
5
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>

2.定义相关配置

1
2
3
4
5
6
7
8
excel.domain=http://192.168.102.1/excel/
pic.domain=http://192.168.102.1/uploadImg/
sftp.host=192.168.102.1
sftp.port=22
sftp.user=root
sftp.password=12341234
sftp.file.path=/home/nginx/html/excel
sftp.image.path=/home/nginx/html/uploadImg

3.实现工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@Slf4j
@Component
public class SFtpUtil {

//ftp服务器ip地址
@Value("${sftp.host}") // 相关配置放在application.properties 中
private String host;
//端口号
@Value("${sftp.port}")
private Integer port;
//用户名
@Value("${sftp.user}")
private String user;
//密码
@Value("${sftp.password}")
private String pwd;
//文件路径
@Value("${sftp.file.path}")
private String filePath;
//图片路径
@Value("${sftp.image.path}")
private String picPath;

public ChannelSftp connect() throws JSchException {

ChannelSftp sftp = null;
try {
Session session = null;
JSch jsch = new JSch();
if (port != null) {
session = jsch.getSession(user, host, port.intValue());
} else {
session = jsch.getSession(user, host);
}
session.setPassword(pwd);
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking", "no");
// 30秒连接超时
session.connect(30000);

Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
System.out.println("SFTPUitl 获取连接发生错误");
throw e;
}
return sftp;
}


public void destroy(ChannelSftp sftp) {
try {
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
if (sftp.getSession() != null) {
sftp.getSession().disconnect();
}
}
}catch (Exception e) {
log.error("",e);
}
}

// 上传文件
public void upload(InputStream ins, String fileName) throws Exception {
upload(filePath,ins,fileName);
}
// 上传图片
public void uploadPic(InputStream ins, String fileName) throws Exception {
upload(picPath,ins,fileName);
}

public void upload(String directory, InputStream ins, String fileName) throws Exception {

ChannelSftp sftp = null;
try {
sftp = connect();
Vector<?> content = sftp.ls(directory);
if (content == null) {
sftp.mkdir(directory);
}
// 进入目标路径
sftp.cd(directory);

sftp.put(ins, new String(fileName.getBytes(), "UTF-8"));

} finally {
destroy(sftp);
}

}
}

4.相关调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 声明
@Autowired
private SFtpUtil sFtpUtil;


// 此处引入相关代码,省略了相关方法内容
String dateStr = DateUtils.getDateStr();
String fileName = "aaa" + dateStr + ".xlsx";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 此处通过EasyExcel工具类将excel内容(resultLs)写到outputStream中
EasyExcel.write(outputStream,ProductExcelDTO.class).head(ProductExcelDTO.class).sheet("测试1").doWrite(resultLs);
// 下面两行,将outputStream转为InputStream,目的是给sFtpUtil.upload传参
byte[] content = outputStream.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 调用sFtpUtil工具类的upload方法将生成的excel文件上传到sftp服务器
sFtpUtil.upload(is, fileName);

上面代码目的是生成个excel文件上传到sftp服务器,供前端下载的时候调用。当文件上传后,将对应的http请求链接返回给前端,前端拿到链接直接请求从而将文件下载下来。怎么配置http请求,就需要运维走nginx进行配置。本例中http链接就是配置文件定义的excel.domain=http://192.168.102.1/excel/ + 对应的文件名。

5.总结

使用sftp上传功能,适用像下载功能这种一次性访问的,或简单的小项目。毕竟sftp服务器不是分布式的。像图片上传这种还是建议走fastzDFS这种分布式的上传工具,上传的图片都会有备份,其中一台出现问题,也能够正常访问。