java文件上傳與文件下載是程序開發中比較常見的功能,下面通過本文給大家介紹Java中實現文件上傳下載的三種解決方案,具體詳情如下所示;
第一點:Java代碼實現文件上傳
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
|
FormFile file=manform.getFile(); String newfileName = null ; String newpathname= null ; String fileAddre= "/numUp" ; try { InputStream stream = file.getInputStream(); // 把文件讀入 String filePath = request.getRealPath(fileAddre); //取系統當前路徑 File file1 = new File(filePath); //添加了自動創建目錄的功能 ((File) file1).mkdir(); newfileName = System.currentTimeMillis() + file.getFileName().substring( file.getFileName().lastIndexOf( '.' )); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream bos = new FileOutputStream(filePath + "/" + newfileName); newpathname=filePath+ "/" +newfileName; System.out.println(newpathname); // 建立一個上傳文件的輸出流 System.out.println(filePath+ "/" +file.getFileName()); int bytesRead = 0 ; byte [] buffer = new byte [ 8192 ]; while ((bytesRead = stream.read(buffer, 0 , 8192 )) != - 1 ) { bos.write(buffer, 0 , bytesRead); // 將文件寫入服務器 } bos.close(); stream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } |
第二點:Jsp頁面上實現文件上傳
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
|
package com.vogoal.util; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Hashtable; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; public class JspFileUpload { /** request對象 */ private HttpServletRequest request = null ; /** 上傳文件的路徑 */ private String uploadPath = null ; /** 每次讀取得字節的大小 */ private static int BUFSIZE = 1024 * 8 ; /** 存儲參數的Hashtable */ private Hashtable paramHt = new Hasptable(); /** 存儲上傳的文件的文件名的ArrayList */ private ArrayList updFileArr = new ArrayList(); /** * 設定request對象。 * * @param request * HttpServletRequest request對象 */ public void setRequest(HttpServletRequest request) { this .request = request; } /** * 設定文件上傳路徑。 * * @param path * 用戶指定的文件的上傳路徑。 */ public void setUploadPath(String path) { this .uploadPath = path; } /** * 文件上傳處理主程序。�������B * * @return int 操作結果 0 文件操作成功;1 request對象不存在。 2 沒有設定文件保存路徑或者文件保存路徑不正確;3 * 沒有設定正確的enctype;4 文件操作異常。 */ public int process() { int status = 0 ; // 文件上傳前,對request對象,上傳路徑以及enctype進行check。 status = preCheck(); // 出錯的時候返回錯誤代碼。 if (status != 0 ) return status; try { // ��參數或者文件名�u�� String name = null ; // 參數的value String value = null ; // 讀取的流是否為文件的標志位 boolean fileFlag = false ; // 要存儲的文件。 File tmpFile = null ; // 上傳的文件的名字 String fName = null ; FileOutputStream baos = null ; BufferedOutputStream bos = null ; // ��存儲參數的Hashtable paramHt = new Hashtable(); updFileArr = new ArrayList(); int rtnPos = 0 ; byte [] buffs = new byte [BUFSIZE * 8 ]; // �取得ContentType String contentType = request.getContentType(); int index = contentType.indexOf( "boundary=" ); String boundary = "--" + contentType.substring(index + 9 ); String endBoundary = boundary + "--" ; // �從request對象中取得流。 ServletInputStream sis = request.getInputStream(); // 讀取1行 while ((rtnPos = sis.readLine(buffs, 0 , buffs.length)) != - 1 ) { String strBuff = new String(buffs, 0 , rtnPos); // 讀取1行數據�n�� if (strBuff.startsWith(boundary)) { if (name != null && name.trim().length() > 0 ) { if (fileFlag) { bos.flush(); baos.close(); bos.close(); baos = null ; bos = null ; updFileArr.add(fName); } else { Object obj = paramHt.get(name); ArrayList al = new ArrayList(); if (obj != null ) { al = (ArrayList) obj; } al.add(value); System.out.println(value); paramHt.put(name, al); } } name = new String(); value = new String(); fileFlag = false ; fName = new String(); rtnPos = sis.readLine(buffs, 0 , buffs.length); if (rtnPos != - 1 ) { strBuff = new String(buffs, 0 , rtnPos); if (strBuff.toLowerCase().startsWith( "content-disposition: form-data; " )) { int nIndex = strBuff.toLowerCase().indexOf( "name=\"" ); int nLastIndex = strBuff.toLowerCase().indexOf( "\"" , nIndex + 6 ); name = strBuff.substring(nIndex + 6 , nLastIndex); } int fIndex = strBuff.toLowerCase().indexOf( "filename=\"" ); if (fIndex != - 1 ) { fileFlag = true ; int fLastIndex = strBuff.toLowerCase().indexOf( "\"" , fIndex + 10 ); fName = strBuff.substring(fIndex + 10 , fLastIndex); fName = getFileName(fName); if (fName == null || fName.trim().length() == 0 ) { fileFlag = false ; sis.readLine(buffs, 0 , buffs.length); sis.readLine(buffs, 0 , buffs.length); sis.readLine(buffs, 0 , buffs.length); continue ; } else { fName = getFileNameByTime(fName); sis.readLine(buffs, 0 , buffs.length); sis.readLine(buffs, 0 , buffs.length); } } } } else if (strBuff.startsWith(endBoundary)) { if (name != null && name.trim().length() > 0 ) { if (fileFlag) { bos.flush(); baos.close(); bos.close(); baos = null ; bos = null ; updFileArr.add(fName); } else { Object obj = paramHt.get(name); ArrayList al = new ArrayList(); if (obj != null ) { al = (ArrayList) obj; } al.add(value); paramHt.put(name, al); } } } else { if (fileFlag) { if (baos == null && bos == null ) { tmpFile = new File(uploadPath + fName); baos = new FileOutputStream(tmpFile); bos = new BufferedOutputStream(baos); } bos.write(buffs, 0 , rtnPos); baos.flush(); } else { System.out.println( "test :" + value + "--" + strBuff); value = value + strBuff; } } } } catch (IOException e) { status = 4 ; } return status; } private int preCheck() { int errCode = 0 ; if ( request == null ) return 1 ; if ( uploadPath == null || uploadPath.trim().length() == 0 ) return 2 ; else { File tmpF = new File(uploadPath); if (!tmpF.exists()) return 2 ; } String contentType = request.getContentType(); if ( contentType.indexOf( "multipart/form-data" ) == - 1 ) return 3 ; return errCode; } public String getParameter(String name){ String value = "" ; if ( name == null || name.trim().length() == 0 ) return value; value = (paramHt.get(name) == null )? "" :(String)((ArrayList)paramHt.get(name)).get( 0 ); return value; } public String[] getParameters(String name){ if ( name == null || name.trim().length() == 0 ) return null ; if ( paramHt.get(name) == null ) return null ; ArrayList al = (ArrayList)paramHt.get(name); String[] strArr = new String[al.size()]; for ( int i= 0 ;i<al.size();i++ ) strArr[i] = (String)al.get(i); return strArr; } public int getUpdFileSize(){ return updFileArr.size(); } public String[] getUpdFileNames(){ String[] strArr = new String[updFileArr.size()]; for ( int i= 0 ;i<updFileArr.size();i++ ) strArr[i] = (String)updFileArr.get(i); return strArr; } private String getFileName(String input){ int fIndex = input.lastIndexOf( "\\" ); if (fIndex == - 1 ) { fIndex = input.lastIndexOf( "/" ); if (fIndex == - 1 ) { return input; } } input = input.substring(fIndex + 1 ); return input; } private String getFileNameByTime(String input){ int index = input.indexOf( "." ); Date dt = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS" ); return input.substring( 0 ,index) + sdf.format(dt) + input.substring(index); } } |
2.在Jsp頁面中進行引用該Java類:
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
|
<% @page import = "com.vogoal.util.JspFileUpload" %> <% //初始化 JspFileUpload jfu = new JspFileUpload(); //設定request對象 jfu.setRequest(request); //設定上傳的文件路徑 jfu.setUploadPath( "C:\\" ); //上傳處理 int rtn = jfu.process(); //取得form中其他input控件參數的值 String username = jfu.getParameter( "username" ); //如果對應同一個參數有多個input控件,返回數組 String[] usernameArr = jfu.getParameters( "username" ); //取得上傳的文件的名字 String[] fileArr = jfu.getUpdFileNames(); //取得上傳文件的個數,這個方法有點雞肋 int fileNumber = jfu.getUpdFileSize(); //下面的是測試輸出的代碼。 // out.println("parameter:" + username); // out.println("parameter size:" + usernameArr.length); // out.println("fileArr size:" + fileArr.length); // if (fileArr.length > 0) // out.println("fileArr 0:" + fileArr[0]); %> |
第三點:struts2實現文件的上傳和下載
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。這兩個文件可以從http://commons.apache.org/下載。
第二步:把form表的enctype設置為:“multipart/form-data“,如下:
Java代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class UploadAction{ private File uploadImage; //文件 private String uploadImageContentType; //文件的類型 private String uploadImageFileName; //文件的名稱 private String bookname; //書名 private String author; //作者 private String savePath; //文件的保存位置 //屬性的getter/setter方法 public String upload() throws Exception{ //實現上傳代碼,I/O操作完成 return "uploadSuccess" ; } } |
注:一個表單里的文件域對應Action中三個屬性,分別是文件,文件名,文件類型,命名是固定的,文件名必須表單中的文件域名稱相同(uploadImage),文件名為:文件+FileName,文件類型:文件+ContentType。
第四步:將我們的上傳Action配置到struts.xml中。
Java代碼
1
2
3
4
|
<action name= "upload" class = "com.gqy.UploadAction" > <param name= "savePath" >/uploadFile</param> <result>/success.jsp</result> </action> |
注:指定上傳文件的在服務器上的保存目錄,需要在UploadAction中為定義savePath變量并為其添加相應的setter和getter方法,便于Struts2將/uploadFile值賦給savePath屬性,即要想在UploadAction中使用savePath變量必須在UploadAction定義。
手動配置文件過濾類型
1
2
3
|
<param name= "allowTypes" > image/bmp,image/png,image/gif,image/jpeg </param> |
手動配置文件大小限制
1
|
<param name= "maximumSize" > 1048576 </param> |
使用Struts2的文件上傳攔截器實現文件過濾
Struts2提供了一個文件上傳的攔截器—fileUpload,通過配置該攔截器可以方便實現上傳文件的過濾。
配置fileUpload攔截器時,可以為其指定兩個參數:
? allowedTypes:指定允許上傳的文件類型,多個文件類型之間以英文逗號(,)隔開。
? maximumSize:指定允許上傳的文件大小,單位是字節。
提示:通過配置fileUpload攔截器,可以輕松的實現文過濾,當文件過濾失敗后,系統自動轉入input邏輯視圖,因此必須為該Action配置名為input的邏輯視圖,除此之外,還必須顯示地為該Action配置defaultStack的攔截器引用。
使用Struts2的攔截器實現文件過濾配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<action name= "uploadFileAction" class = "com.actions.UploadFileAction" > <interceptor-ref name= "defaultStack" > <!-- 配置允許上傳的文件類型,多個用 "," 分隔 --> <param name= "fileUpload.allowedTypes" > image/bmp,image/png,image/gif,image/jpeg,image/jpg ,image/x-png, image/pjpeg </param> <!-- 配置允許上傳的文件大小,單位字節,本例為:1MB --> <param name= "fileUpload.maximumSize" > 1048576 </param> </interceptor-ref> <result name= "input" >/jsp/oneFileFileupload.jsp</result> <result name= "success" >/jsp/result.jsp</result> </action> |
當用戶上傳失敗后,需要有一定的提示信息。在Struts2中,使用<s:fielderror/>標簽即可將錯誤提示信息輸出到頁面中。
注:要想使用Struts2錯誤提示信息,則上傳文件的Action類,必須繼承ActionSupport,否則Struts2不會提供輸出錯誤提示信息功能。
我們可以配置資源文件(.properties)來保存輸出給用戶的信息。
struts.messages.eror.file.too.large:當上傳文件大小超過設定的值時,Struts2將輸出該key對應的提示信息。
struts.messages.error.content.type.not.allowed:當上傳文件類型不符合設定的值時,Struts2將輸出該key對應的提示信息。
struts.messages.error.uploading:當上傳文件時出現未知錯誤時,Struts2將輸出該key對應的提示信息。
我們還要將資源文件配置到struts.xml文件中,接下來看看我們的資源文件,已經包含中文了,得把它進行一下轉換再配置到工程中。
在struts.xml中設定資源文件:
1
2
|
<constant name= "struts.custom.i18n.resources" value= "messages" />或 <constant name= "struts.custom.i18n.resources" value= "messages_zh_CN" /> |
用命令native2ascii d:\messages.properties d:\messages_zh_CN.properties將原有的資源文件轉換成支持中的。
注:保持國際化,資源文件的名稱后綴為: *_zh_CN+文件擴展名的形式。
對于多個文件上傳的原理同上,但是需要注意的是,多個文件域的name屬性名必須相同,而且在Action中應該使用File [] 或者List<File>來接收。
個人覺得用這樣的方式進行多個文件上傳不是
很好。
Struts2進行文件下載
Struts2提供了stream結果類型,該結果類型專門用于支持文件下載的功能。當指定stream結果類型時,需要配置一個inputName參數,該參數指定了一個輸入流,這個輸入流是被下載文件的入口(即通過該入口才能實現文件以流的方式實現下載)。
實現文件下載的Action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class FileDownloadAction implements Action{ //該屬性值在配置文件中指定,Struts2會自動進行注入(即賦值),需要為該屬性提供setter和 getter方法 private String inputPath; //指定要下載的文件的完整路徑(路徑名+文件名) /* * 實現下載的Action類應該提供一個返回InputStream實例的方法,該方法對應在 <result.../>里的inputName屬性值為targetFile */ public InputStream getTargetFile() throws Exception{ return ServletActionContext.getServletContext().getResourceAsStream(inputPath); } //處理用戶請求的execute方法,該方法返回success字符串 public String execute() throws Exception{ return "success" ; } } |
對應Action在struts.xml文件中的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<action name= "download" class = "com.FileDownloadAction" > <!--指定被下載資源的位置--> <param name= "inputPath" >/uploadFile/demo.txt</param> <!--配置結果類型為stream的結果--> <result name= "success" type= "stream" > <!--指定下載文件的文件類型--> <param name= "contentType" ></param> <!--指定下載文件的文件位置--> <param name= "inputName" >targetFile</param> <!--指定下載文件的下載方式及下載時的保存文件名,filename保存時的文件名必須有擴展名,擴展名指示了下載類型的圖標--> <param name= "contentDisposition" > attachment;filename=Struts2.txt </param> <!--指定下載文件的緩沖區大小--> <param name= "bufferSize" > 4096 </param> </result> </action> |
以上所述是小編給大家介紹的Java中實現文件上傳下載的三種解決方案,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!