前幾天做個項目,本身客戶端和管理員端是寫在一起的,共用一臺服務器,客戶上傳的文件都是存在服務器的硬盤上的。老龍提出要把客戶端和管理員端分離,這時候用戶上傳的附件的存儲就出現問題了。顯然,把大到幾百M的apk文件存到數據庫不現實,查了半天,在兩端建立ftp服務器傳文件是最快的方法。
具體流程是,用戶登錄外網客戶端,上傳文件到外網的服務器硬盤上,在此同時,文件通過外網服務器訪問內網管理員服務器的ftp服務器,傳到內網服務器的硬盤上。這樣內網服務器可以對上傳的文件進行加密簽名工作,之后也通過ftp的方式把文件回傳到外網服務器硬盤上,供用戶進行其他操作。
具體實現時用到的工具:Serv-U。Serv-U是一個方便我們在windows上建立ftp服務器的工具。下載破解后,按照步驟,設置好Ip、端口、賬戶密碼、允許ftp訪問的磁盤路徑、操作權限等,就可以使用了。IP在本機測試的時候就選127.0.0.1,內網測試時就選192.168.0.x。
在java項目中的實現,我自己寫了個工具類,用到了apache的commons-net包,有上傳,下載以及刪除功能。附上代碼:
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
|
package app.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; 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; /** * FTP服務器工具類 * */ public class FTPUtils { /** * 上傳文件至FTP服務器 * * @param url * 服務器IP地址 * @param port * 服務器端口 * @param userName * 用戶登錄名 * @param password * 用戶登錄密碼 * @param storePath * 服務器文件存儲路徑 * @param fileName * 服務器文件存儲名稱 * @param is * 文件輸入流 * @return * <b>true</b>:上傳成功 * <br/> * <b>false</b>:上傳失敗 */ public static boolean storeFile (String url, int port, String userName, String password, String storePath, String fileName, InputStream is) { boolean result = false ; FTPClient ftp = new FTPClient(); try { // 連接至服務器,端口默認為21時,可直接通過URL連接 ftp.connect(url ,port); // 登錄服務器 ftp.login(userName, password); // 判斷返回碼是否合法 if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { // 不合法時斷開連接 ftp.disconnect(); // 結束程序 return result; } // 判斷ftp目錄是否存在,如果不存在則創建目錄,包括創建多級目錄 String s = "/" +storePath; String[] dirs = s.split( "/" ); ftp.changeWorkingDirectory( "/" ); //按順序檢查目錄是否存在,不存在則創建目錄 for ( int i= 1 ; dirs!= null &&i<dirs.length; i++) { if (!ftp.changeWorkingDirectory(dirs[i])) { if (ftp.makeDirectory(dirs[i])) { if (!ftp.changeWorkingDirectory(dirs[i])) { return false ; } } else { return false ; } } } // 設置文件操作目錄 ftp.changeWorkingDirectory(storePath); // 設置文件類型,二進制 ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // 設置緩沖區大小 ftp.setBufferSize( 3072 ); // 上傳文件 result = ftp.storeFile(fileName, is); // 關閉輸入流 is.close(); // 登出服務器 ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 判斷輸入流是否存在 if ( null != is) { // 關閉輸入流 is.close(); } // 判斷連接是否存在 if (ftp.isConnected()) { // 斷開連接 ftp.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return result; } /** * 從FTP服務器下載文件至本地 * * @param url * 服務器IP地址 * @param port * 服務器端口 * @param userName * 用戶登錄名 * @param password * 用戶登錄密碼 * @param remotePath * 服務器文件存儲路徑 * @param fileName * 服務器文件存儲名稱 * @param localPath * 本地文件存儲路徑 * @return * <b>true</b>:下載成功 * <br/> * <b>false</b>:下載失敗 */ public static boolean retrieveFile (String url, int port, String userName, String password, String remotePath, String fileName, String localPath) { boolean result = false ; FTPClient ftp = new FTPClient(); OutputStream os = null ; try { // 連接至服務器,端口默認為21時,可直接通過URL連接 ftp.connect(url ,port); // 登錄服務器 ftp.login(userName, password); // 判斷返回碼是否合法 if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { // 不合法時斷開連接 ftp.disconnect(); // 結束程序 return result; } // 設置文件操作目錄 ftp.changeWorkingDirectory(remotePath); // 設置文件類型,二進制 ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // 設置緩沖區大小 ftp.setBufferSize( 3072 ); // 設置字符編碼 ftp.setControlEncoding( "UTF-8" ); // 構造本地文件對象 File localFile = new File(localPath + "/" + fileName); // 獲取文件操作目錄下所有文件名稱 String[] remoteNames = ftp.listNames(); // 循環比對文件名稱,判斷是否含有當前要下載的文件名 for (String remoteName: remoteNames) { if (fileName.equals(remoteName)) { result = true ; } } // 文件名稱比對成功時,進入下載流程 if (result) { // 構造文件輸出流 os = new FileOutputStream(localFile); // 下載文件 result = ftp.retrieveFile(fileName, os); // 關閉輸出流 os.close(); } // 登出服務器 ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 判斷輸出流是否存在 if ( null != os) { // 關閉輸出流 os.close(); } // 判斷連接是否存在 if (ftp.isConnected()) { // 斷開連接 ftp.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return result; } /** * 從FTP服務器刪除文件 * * @param url * 服務器IP地址 * @param port * 服務器端口 * @param userName * 用戶登錄名 * @param password * 用戶登錄密碼 * @param remotePath * 服務器文件存儲路徑 * @param fileName * 服務器文件存儲名稱 * @return * <b>true</b>:刪除成功 * <br/> * <b>false</b>:刪除失敗 */ public static boolean deleteFile (String url, int port, String userName, String password, String remotePath, String fileName) { boolean result = false ; FTPClient ftp = new FTPClient(); try { // 連接至服務器,端口默認為21時,可直接通過URL連接 ftp.connect(url ,port); // 登錄服務器 ftp.login(userName, password); // 判斷返回碼是否合法 if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { // 不合法時斷開連接 ftp.disconnect(); // 結束程序 return result; } // 設置文件操作目錄 ftp.changeWorkingDirectory(remotePath); // 設置文件類型,二進制 ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // 設置緩沖區大小 ftp.setBufferSize( 3072 ); // 設置字符編碼 ftp.setControlEncoding( "UTF-8" ); // 獲取文件操作目錄下所有文件名稱 String[] remoteNames = ftp.listNames(); // 循環比對文件名稱,判斷是否含有當前要下載的文件名 for (String remoteName: remoteNames) { if (fileName.equals(remoteName)) { result = true ; } } // 文件名稱比對成功時,進入刪除流程 if (result) { // 刪除文件 result = ftp.deleteFile(fileName); } // 登出服務器 ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 判斷連接是否存在 if (ftp.isConnected()) { // 斷開連接 ftp.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return result; } public static void main(String[] args) throws FileNotFoundException { // try { // FileInputStream fis = new FileInputStream(new File("D:/Soft Storage/軟件工具箱/HTML_Help_WorkShop_1.3_XiaZaiBa.zip")); // System.out.println(storeFile("192.168.1.2", 21, "admin", "1", "C:/Documents and Settings/Administrator/桌面", RandomUUID.random() + ".zip", fis)); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } // //File file = new File("C:/Users/freed/Desktop/1.txt"); //InputStream is = new FileInputStream(file); //System.out.println(storeFile("127.0.0.1", 21, "feili", "feili", "examples", "2.txt", is)); //System.out.println(retrieveFile("127.0.0.1", 21, "feili", "feili", "examples/jsp", "index.html", "C:/Users/freed/Desktop")); //System.out.println(deleteFile("127.0.0.1", 21, "feili", "feili", "testpath", "1.txt")); } } |
需要注意的是上傳文件的時候要將File文件先放入fileinputstream中。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/wu_fei_li/article/details/69949928