SFTPClient.java
6.14 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package com.sitech.util.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.ChannelSftp.LsEntry;
/**
* SFTP工具类
*
* @author LINXC
*/
public class SFTPClient implements FTPIF{
private Logger logger = Logger.getLogger(SFTPClient.class);
private ChannelSftp sftp = null;
private Session sshSession = null;
public void login(String host, String username, String password)
throws Exception {
this.connect(host, 22, username, password);
}
public boolean store(String uploadFile, String toRfileName)
throws Exception {
String directory = "";
if (toRfileName.lastIndexOf("/") != -1) {
directory = toRfileName.substring(0, toRfileName.lastIndexOf("/"));
}
return upload(directory, uploadFile);
}
/***************************************************************************
* 连接SFTP服务器
*
* @param host
* 主机
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
* @return
* @throws Exception
**************************************************************************/
public boolean connect(String host, int port, String username,
String password) throws Exception {
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
logger.info("Create sftp connection successful. ");
return true;
} catch (Exception e) {
throw e;
}
}
/***************************************************************************
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @param sftp
**************************************************************************/
public boolean upload(String directory, String uploadFile) {
InputStream is = null;
try {
if (directory != "") {
sftp.cd(directory);
}
File file = new File(uploadFile);
is = new FileInputStream(file);
sftp.put(is, file.getName());
logger.info("Upload file[" + uploadFile + "] successful. ");
return true;
} catch (Exception e) {
logger.error("Upload file[" + uploadFile + "] failed. ", e);
return false;
}finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/***************************************************************************
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
* @param sftp
**************************************************************************/
public void download(String directory, String downloadFile,
String saveFile) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
logger.info("Download file[" + downloadFile + "] successful. ");
} catch (Exception e) {
e.printStackTrace();
logger.info("Download file[" + downloadFile + "] failed. ");
}
}
/***************************************************************************
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param sftp
**************************************************************************/
public void delete(String directory, String deleteFile) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
logger.info("Delete file[" + deleteFile + "] successful. ");
} catch (Exception e) {
logger.info("Delete file[" + deleteFile + "] failed. ");
}
}
/***************************************************************************
* 列出目录下的文件
*
* @param directory
* 要列出的目录
* @param sftp
* @return
* @throws SftpException
**************************************************************************/
@SuppressWarnings("unchecked")
public Vector<LsEntry> listFiles(String directory)
throws SftpException {
return sftp.ls(directory);
}
public void logout() throws Exception {
if(sftp != null){
sftp.disconnect();
}
if(sshSession != null){
sshSession.disconnect();
}
}
public boolean chdir(String remoteDir) throws Exception {
try{
sftp.cd(remoteDir);
}catch (Exception e) {
throw e;
}
return true;
}
public RemoteFile[] listRemoteFile(String directory) throws Exception {
Vector<LsEntry> temp = listFiles(directory);
if (temp == null || temp.size() == 0) {
return new RemoteFile[] {};
}
RemoteFile[] file = new RemoteFile[temp.size()];
for (int i = 0; i < temp.size(); i++) {
file[i] = new RemoteFile(temp.get(i));
}
return file;
}
public RemoteFile listRemoteFile(String directory, String fileName) throws Exception {
Vector<LsEntry> temp = listFiles(directory);
if (temp == null || temp.size() == 0) {
return null;
}
for(LsEntry entry : temp){
if(entry.getFilename().equals(fileName)){
return new RemoteFile(entry);
}
}
return null;
}
public boolean get(String fileName, String localFileName) throws Exception {
File file = new File(localFileName);
FileOutputStream fos = new FileOutputStream(file);
sftp.get(fileName, fos);
fos.close();
return true;
}
public boolean rename(String fromName, String toName) throws Exception {
try {
sftp.rename(fromName, toName);
return true;
} catch (Exception e) {
throw e;
}
}
}