SpringBoot 实现 Sftp 上传下载

  1. 依赖
1
2
3
4
5
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
  1. 工具类
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cn.likfees.common.utils.file;

import cn.likfees.common.config.DockerConfig;
import com.jcraft.jsch.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Properties;
import java.util.Vector;

/**
* @Auther: Likfees
* @Date: 2022/12/13 12:23
* @Description: Sftp 工具类
*/
@Component
public class SftpUtils {

@Autowired
private DockerConfig dockerConfig;

private Session session = null;

public void connect() {

JSch jSch = new JSch();
try {
session = jSch.getSession(dockerConfig.getUsername(), dockerConfig.getDockerHost());
session.setPassword(dockerConfig.getPassword());
Properties config = new Properties();
// SSH 公钥检查机制 no、ask、yes
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);// 为Session对象设置properties
session.setTimeout(30000);
session.connect();// 通过Session建立连接
} catch (JSchException e) {
throw new RuntimeException(String.format("SFTP getSession() 失败! host: %s; username: %s, password: %s, err_msg: %s", dockerConfig.getDockerHost(), dockerConfig.getUsername(), dockerConfig.getPassword(), e.getMessage()));
}

}

/**
* 执行相关的命令,
* 但是部分情况不可用
* @param command 命令
* @throws JSchException
*/
public void execCmd(String command) throws JSchException {
BufferedReader reader = null;

Channel channel = null;
try {
if (command != null) {
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// ((ChannelExec) channel).setErrStream(System.err);
channel.connect();

InputStream in = channel.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
System.out.println(buf);
}
}
} catch (IOException | JSchException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
if (channel != null) {
channel.disconnect();
}
}
}



/**
* 上传文件
*
* @param uploadFile 要上传的文件
* @param directory 上传的目录
* @throws JSchException
* @throws SftpException
* @throws FileNotFoundException
*/
public void upload(String uploadFile, String directory) throws JSchException, SftpException, FileNotFoundException {
ChannelSftp channelSftp = null;
try {
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.cd(directory);
File file = new File(uploadFile);
channelSftp.put(new FileInputStream(file), file.getName());
} finally {
assert channelSftp != null;
channelSftp.disconnect();
}

}

/**
* 下载文件
*
* @param src linux服务器文件地址
* @param dst 本地存放地址
* @throws JSchException
* @throws SftpException
*/
public void download(String src, String dst) throws Exception {
ChannelSftp channelSftp = null;
try {
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(src, dst);
} finally {
assert channelSftp != null;
channelSftp.disconnect();
}
}

/**
* 查看路径下的所有文件
*
* @param path 路径
*/
public Vector listFiles(String path) throws Exception {
ChannelSftp channelSftp = null;
try {
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
return channelSftp.ls(path);
} finally {
assert channelSftp != null;
channelSftp.disconnect();
}
}


/**
* 删除文件
*
* @param directory 要删除文件所在目录
* @param deleteFile 要删除的文件
* @throws SftpException
* @throws JSchException
*/
public void delete(String directory, String deleteFile) throws Exception {
ChannelSftp channelSftp = null;
try {
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.cd(directory);
channelSftp.rm(deleteFile);
} finally {
assert channelSftp != null;
channelSftp.disconnect();
}
}

/**
* 关闭连接
*/
public void close() {
if (session != null) {
session.disconnect();
}
}
}

  1. 简单使用
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
/**
* @Auther: Likfees
* @Date: 2022/12/13 15:12
* @Description:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class SftpUtilsTest {

@Autowired
private SftpUtils sftpUtils;

@Test
public void testExec() throws JSchException {
sftpUtils.connect();
sftpUtils.execCmd("ls");
sftpUtils.close();
}

@Test
public void testUpload() throws JSchException, SftpException, FileNotFoundException {
sftpUtils.connect();

sftpUtils.upload("D:\\Code\\Java\\feng-code\\feng-code-server\\feng-code-web\\src\\main\\resources\\banner.txt", "/root");

sftpUtils.close();
}


@Test
public void testListFiles() throws Exception {
sftpUtils.connect();
Vector lsEntries = sftpUtils.listFiles("/root");
System.out.println(lsEntries);
sftpUtils.close();
}

}