-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFtpUtil.java
More file actions
276 lines (252 loc) · 8.98 KB
/
FtpUtil.java
File metadata and controls
276 lines (252 loc) · 8.98 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package cn.sinobest.ypgj.util;
import cn.sinobest.jzpt.framework.utils.JdbcService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author chenjianhua
* @date 2014/03/27
* ftp工具类,依赖org.apache.commons.net.ftp.FTPClient实现
*/
public class FtpUtil {
private FTPClient ftpClient = new FTPClient();
private static Map<String,String> para = new HashMap<String, String>();
private String rootPath;
private static FtpUtil ftpUtil = null;
static{
}
public static FtpUtil getInstance() {
if (ftpUtil == null) {
synchronized (FtpUtil.class) {
ftpUtil = new FtpUtil();
}
}
return ftpUtil;
}
private FtpUtil() {
/* 初始化ftp配置信息 */
List<Map<String,String>> list = JdbcService.getInstance().queryForList("SELECT code,value FROM s_parameter WHERE CATEGORY=?", new Object[]{"FTP"}, new int[]{Types.VARCHAR});
for(Map<String,String> entry : list){
para.put(entry.get("CODE"),entry.get("VALUE"));
}
rootPath = para.get("ftpPath");
}
/**
* 连接ftp服务器
*/
public boolean connect() {
String ipAddr = para.get("ftpIpAddr");
String username = para.get("ftpUser");
String password = para.get("ftpPassword");
int port = 0;
if(para.get("ftpPort")!=null && !StringUtils.isEmpty(para.get("ftpPort"))){
port = Integer.parseInt(para.get("ftpPort"));
}
try {
if(port > 0){
ftpClient.connect(ipAddr,port);
}else{
ftpClient.connect(ipAddr);
}
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username, password)){
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.makeDirectory(this.rootPath);
ftpClient.changeWorkingDirectory(this.rootPath);
System.out.println("连接ftp服务器成功...");
return true;
}else{
throw new RuntimeException("登录ftp文件服务器失败!请检查配置!");
}
}else{
throw new RuntimeException("连接ftp文件服务器失败!请检查配置!");
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 与ftp服务器断开
*/
public void disconnect(){
if(ftpClient != null && ftpClient.isConnected()){
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 递归创建远程服务器目录
* @param dirPath 远程服务器文件绝对路径
* @return 目录创建是否成功
* @throws IOException
*/
public boolean CreateDirecroty(String dirPath) throws IOException{
boolean status = true;
String directory = dirPath.substring(0, dirPath.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"),"iso-8859-1"))){
//如果远程目录不存在,则递归创建远程服务器目录
int start=0;
int end = 0;
if(directory.startsWith("/")){
start = 1;
}else{
start = 0;
}
end = directory.indexOf("/",start);
while(true){
String subDirectory = new String(dirPath.substring(start,end).getBytes("GBK"),"iso-8859-1");
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else {
System.out.println("创建目录失败");
return false;
}
}
start = end + 1;
end = directory.indexOf("/",start);
//检查所有目录是否创建完毕
if(end <= start){
break;
}
}
}
return status;
}
/**
* 上传文件到ftp指定目录
* @param content
* @param dirPath
* @param fileName
* @return
*/
public boolean upload(byte[] content,String dirPath,String fileName){
if(content!=null && content.length > 0 && !StringUtils.isEmpty(fileName)){
ByteArrayInputStream bais = null;
try {
ftpClient.changeWorkingDirectory("/");
if(!StringUtils.isEmpty(dirPath)){
dirPath = this.rootPath + dirPath;
}else{
dirPath = this.rootPath;
}
if(!ftpClient.changeWorkingDirectory(dirPath)){
if(CreateDirecroty(dirPath)){
System.out.println("ftp创建目录成功");
ftpClient.changeWorkingDirectory(dirPath);
}else {
System.out.println("ftp创建目录失败");
}
}
ftpClient.setBufferSize(1024*4);
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
bais = new ByteArrayInputStream(content);
ftpClient.storeFile(fileName, bais);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}else{
return false;
}
}
/**
* 从ftp服务器下载文件
* @param dirPath
* @param fileName
* @return
* @throws IOException
*/
public byte[] download(String dirPath,String fileName) throws IOException {
//校验参数
if(StringUtils.isEmpty(fileName)){
return null;
}
InputStream inputStream = null;
String ipAddr = para.get("ftpIpAddr");
String username = para.get("ftpUser");
String password = para.get("ftpPassword");
int port = 0;
if(para.get("ftpPort")!=null && !StringUtils.isEmpty(para.get("ftpPort"))){
port = Integer.parseInt(para.get("ftpPort"));
}
//1.连接登录ftp文件服务器
FTPClient ftpClient1 = new FTPClient();
try {
if(port > 0){
ftpClient1.connect(ipAddr,port);
}else{
ftpClient1.connect(ipAddr);
}
if(FTPReply.isPositiveCompletion(ftpClient1.getReplyCode())){
if(ftpClient1.login(username, password)){
ftpClient1.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient1.makeDirectory(this.rootPath);
ftpClient1.changeWorkingDirectory(this.rootPath);
}else{
throw new RuntimeException("登录ftp文件服务器失败!请检查配置!");
}
}else{
throw new RuntimeException("连接ftp文件服务器失败!请检查配置!");
}
} catch (IOException e) {
e.printStackTrace();
}
ftpClient1.changeWorkingDirectory("/");
if(!StringUtils.isEmpty(dirPath)){
dirPath = this.rootPath + dirPath;
}else{
dirPath = this.rootPath;
}
ftpClient1.changeWorkingDirectory(dirPath);
ftpClient1.setBufferSize(1024 * 4);
//设置以二进制方式传输
ftpClient1.setFileType(ftpClient1.BINARY_FILE_TYPE);
//2.获取文件流
inputStream = ftpClient1.retrieveFileStream(fileName);
if(inputStream == null){
System.err.println("ftp服务器没有该文件!");
return null;
}
//3.把InputStream里面的数据转储到byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] b = new byte[1024];
while ((len = inputStream.read(b, 0, b.length)) != -1) {
baos.write(b, 0, len);
}
byte[] buffer = baos.toByteArray();
//4.关闭
inputStream.close();
if(ftpClient1 != null && ftpClient1.isConnected()){
try {
ftpClient1.logout();
ftpClient1.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer;
}
/**
* 下载根目录下的文件
* @param fileName
* @return
* @throws IOException
*/
public byte[] download(String fileName) throws IOException {
return download(null, fileName);
}
}