具體詳細(xì)介紹請看下文:
在使用文件進(jìn)行交互數(shù)據(jù)的應(yīng)用來說,使用FTP服務(wù)器是一個很好的選擇。本文使用Apache Jakarta Commons Net(commons-net-3.3.jar) 基于FileZilla Server服務(wù)器實(shí)現(xiàn)FTP服務(wù)器上文件的上傳/下載/刪除等操作。
關(guān)于FileZilla Server服務(wù)器的詳細(xì)搭建配置過程,詳情請見 FileZilla Server安裝配置教程 。之前有朋友說,上傳大文件(幾百M(fèi)以上的文件)到FTP服務(wù)器時會重現(xiàn)無法重命名的問題,但本人親測上傳2G的文件到FileZilla Server都沒有該問題,朋友們可以放心使用該代碼。
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
|
FavFTPUtil.Java package com.favccxx.favsoft.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FavFTPUtil { /** * 上傳文件(可供Action/Controller層使用) * @param hostname FTP服務(wù)器地址 * @param port FTP服務(wù)器端口號 * @param username FTP登錄帳號 * @param password FTP登錄密碼 * @param pathname FTP服務(wù)器保存目錄 * @param fileName 上傳到FTP服務(wù)器后的文件名稱 * @param inputStream 輸入文件流 * @return */ public static boolean uploadFile(String hostname, int port, String username, String password, String pathname, String fileName, InputStream inputStream){ boolean flag = false ; FTPClient ftpClient = new FTPClient(); ftpClient.setControlEncoding( "UTF-8" ); try { //連接FTP服務(wù)器 ftpClient.connect(hostname, port); //登錄FTP服務(wù)器 ftpClient.login(username, password); //是否成功登錄FTP服務(wù)器 int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)){ return flag; } ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.makeDirectory(pathname); ftpClient.changeWorkingDirectory(pathname); ftpClient.storeFile(fileName, inputStream); inputStream.close(); ftpClient.logout(); flag = true ; } catch (Exception e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()){ try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return flag; } /** * 上傳文件(可對文件進(jìn)行重命名) * @param hostname FTP服務(wù)器地址 * @param port FTP服務(wù)器端口號 * @param username FTP登錄帳號 * @param password FTP登錄密碼 * @param pathname FTP服務(wù)器保存目錄 * @param filename 上傳到FTP服務(wù)器后的文件名稱 * @param originfilename 待上傳文件的名稱(絕對地址) * @return */ public static boolean uploadFileFromProduction(String hostname, int port, String username, String password, String pathname, String filename, String originfilename){ boolean flag = false ; try { InputStream inputStream = new FileInputStream( new File(originfilename)); flag = uploadFile(hostname, port, username, password, pathname, filename, inputStream); } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 上傳文件(不可以進(jìn)行文件的重命名操作) * @param hostname FTP服務(wù)器地址 * @param port FTP服務(wù)器端口號 * @param username FTP登錄帳號 * @param password FTP登錄密碼 * @param pathname FTP服務(wù)器保存目錄 * @param originfilename 待上傳文件的名稱(絕對地址) * @return */ public static boolean uploadFileFromProduction(String hostname, int port, String username, String password, String pathname, String originfilename){ boolean flag = false ; try { String fileName = new File(originfilename).getName(); InputStream inputStream = new FileInputStream( new File(originfilename)); flag = uploadFile(hostname, port, username, password, pathname, fileName, inputStream); } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 刪除文件 * @param hostname FTP服務(wù)器地址 * @param port FTP服務(wù)器端口號 * @param username FTP登錄帳號 * @param password FTP登錄密碼 * @param pathname FTP服務(wù)器保存目錄 * @param filename 要刪除的文件名稱 * @return */ public static boolean deleteFile(String hostname, int port, String username, String password, String pathname, String filename){ boolean flag = false ; FTPClient ftpClient = new FTPClient(); try { //連接FTP服務(wù)器 ftpClient.connect(hostname, port); //登錄FTP服務(wù)器 ftpClient.login(username, password); //驗(yàn)證FTP服務(wù)器是否登錄成功 int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)){ return flag; } //切換FTP目錄 ftpClient.changeWorkingDirectory(pathname); ftpClient.dele(filename); ftpClient.logout(); flag = true ; } catch (Exception e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()){ try { ftpClient.logout(); } catch (IOException e) { } } } return flag; } /** * 下載文件 * @param hostname FTP服務(wù)器地址 * @param port FTP服務(wù)器端口號 * @param username FTP登錄帳號 * @param password FTP登錄密碼 * @param pathname FTP服務(wù)器文件目錄 * @param filename 文件名稱 * @param localpath 下載后的文件路徑 * @return */ public static boolean downloadFile(String hostname, int port, String username, String password, String pathname, String filename, String localpath){ boolean flag = false ; FTPClient ftpClient = new FTPClient(); try { //連接FTP服務(wù)器 ftpClient.connect(hostname, port); //登錄FTP服務(wù)器 ftpClient.login(username, password); //驗(yàn)證FTP服務(wù)器是否登錄成功 int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)){ return flag; } //切換FTP目錄 ftpClient.changeWorkingDirectory(pathname); FTPFile[] ftpFiles = ftpClient.listFiles(); for (FTPFile file : ftpFiles){ if (filename.equalsIgnoreCase(file.getName())){ File localFile = new File(localpath + "/" + file.getName()); OutputStream os = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), os); os.close(); } } ftpClient.logout(); flag = true ; } catch (Exception e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()){ try { ftpClient.logout(); } catch (IOException e) { } } } return flag; } } FavFTPUtilTest.java package com.favccxx.favsoft.util; import junit.framework.TestCase; public class FavFTPTest extends TestCase { public void testFavFTPUtil(){ String hostname = "127.0.0.1" ; int port = 21 ; String username = "business" ; String password = "business" ; String pathname = "business/ebook" ; String filename = "big.rar" ; String originfilename = "C:\\Users\\Downloads\\Downloads.rar" ; FavFTPUtil.uploadFileFromProduction(hostname, port, username, password, pathname, filename, originfilename); // String localpath = "D:/"; // FavFTPUtil.downloadFile(hostname, port, username, password, pathname, filename, localpath); } } |
以上就是JAVA通過FTP上傳下載文件的完整代碼,希望對大家有所幫助。