實例如下:
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
277
278
279
280
281
282
283
284
285
286
287
288
289
|
packagecom.itv.launcher.util; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; importjava.net.InetSocketAddress; importjava.util.Properties; importjava.util.StringTokenizer; importsun.net.TelnetOutputStream; importsun.net.ftp.FtpClient; importsun.net.ftp.FtpProtocolException; /** * FTP上傳工具類 * * @author yanzhou * @version v1.0 */ publicclass FTPUtil { privatestatic FtpClient ftpClient = null; privatestatic final String url; privatestatic final int port; privatestatic final String user; privatestatic final String password; privatestatic final String remoteFilePath; static{ Properties FTPPro = ReadFTPProperties.getMsgFromPro(); url = FTPPro.getProperty("FTP_URL"); port = Integer.parseInt(FTPPro.getProperty("FTP_PORT")); user = FTPPro.getProperty("FTP_USER"); password = FTPPro.getProperty("FTP_PASSWORD"); remoteFilePath = FTPPro.getProperty("FTP_REMOTE_FILEPATH"); } /** * 鏈接FTP * @throws FtpProtocolException */ privatestatic void connectFTP() throwsFtpProtocolException { try{ ftpClient = FtpClient.create(); ftpClient.connect(newInetSocketAddress(url, port)); ftpClient.login(user, password.toCharArray()); ftpClient.setBinaryType(); if(!"".equals(remoteFilePath) && remoteFilePath != null) { ftpClient.changeDirectory(remoteFilePath); } }catch(IOException e) { e.printStackTrace(); } } /** * 關閉FTP鏈接 */ publicstatic void closeFTP() { try{ if(ftpClient != null) { ftpClient.close(); } }catch(IOException e) { e.printStackTrace(); } } /** * 上傳文件到FTP * @param file file文件,struts2從頁面得到的File類型 * @param filePath 要保存在FTP上的路徑(文件夾) * @param fileName 文件名(test001.jpg) * @return 文件是否上傳成功 * @throws Exception */ publicstatic boolean upload(File file, String filePath, String fileName) { TelnetOutputStream to = null; FileInputStream fi = null; filePath = remoteFilePath + Constants.FILE_SEPARATOR + filePath; try{ if(file != null) { connectFTP(); if(!isDirExist(filePath.replace("\\","/"))) { createDir(filePath.replace("\\","/")); ftpClient.changeDirectory(filePath.replace("\\","/")); } fi = newFileInputStream(file); to = (TelnetOutputStream) ftpClient.putFileStream(fileName, true); byte[] bytes = newbyte[1024]; inti = fi.read(bytes); while(i != -1) { to.write(bytes); i = fi.read(bytes); } } returntrue; }catch(FileNotFoundException e1) { returnfalse; }catch(IOException e2) { returnfalse; }catch(Exception e) { returnfalse; }finally{ if(fi != null) { try{ fi.close(); }catch(IOException e) { e.printStackTrace(); } } if(to != null) { try{ to.flush(); to.close(); }catch(IOException e) { e.printStackTrace(); } } closeFTP(); } } /** * 刪除FTP制定目錄下的文件 * @param filePath 文件在FTP存儲的路徑 * @param fileName 要刪除的文件名稱 * @return 是否刪除成功 */ publicstatic boolean deleteFileFtp(String filePath, String fileName){ try{ connectFTP(); filePath = remoteFilePath + Constants.FILE_SEPARATOR + filePath + Constants.FILE_SEPARATOR; if(!isDirExist(filePath.replace("\\","/"))) { returnfalse; } ftpClient.changeDirectory(filePath.replace("\\","/")); ftpClient.deleteFile(fileName); returntrue; }catch(Exception e) { e.printStackTrace(); returnfalse; }finally{ closeFTP(); } } /** * 檢查文件夾是否存在 * * @param dir * @param ftpClient * @return */ privatestatic Boolean isDirExist(String dir) { try{ ftpClient.changeDirectory(dir); }catch(Exception e) { returnfalse; } returntrue; } /** * 創建文件夾 * * @param dir * @param ftpClient * @throws Exception */ privatestatic void createDir(String dir) throwsException { ftpClient.setAsciiType(); StringTokenizer s = newStringTokenizer(dir, "/");// sign s.countTokens(); String pathName = ""; while(s.hasMoreElements()) { pathName = pathName + "/"+ (String) s.nextElement(); try{ ftpClient.makeDirectory(pathName); }catch(Exception e) { e = null; } } ftpClient.setBinaryType(); } } |
2. 常量類,系統的路徑分隔符
1
2
3
4
5
6
7
8
9
|
packagecom.itv.launcher.util; publicinterface Constants { //路徑分隔符 publicstatic String FILE_SEPARATOR = System.getProperty("file.separator"); } |
3. FTP鏈接的配置properties文件,包括用戶名密碼一些信息
1
2
3
4
5
6
7
8
9
10
|
#FTP的IP地址 FTP_URL=127.0.0.1 #FTP端口號 FTP_PORT=1234 #用戶名 FTP_USER=yanzhou #密碼 FTP_PASSWORD=abcdefg12345 #FTP賬號目錄 FTP_REMOTE_FILEPATH= |
以上這篇JDK1.7以上javaFTP上傳刪除文件的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/z69183787/article/details/50481205