文件上傳
-
表單準備
- 要想使用 HTML 表單上傳一個或多個文件
-
須把 HTML 表單的 enctype 屬性設置為
multipart/form-data
- 須把 HTML 表單的method 屬性設置為 post
-
需添加
<input type=“file”>
字段.
-
Struts 對文件上傳的支持
- 在 Struts 應用程序里, FileUpload 攔截器和 Jakarta Commons FileUpload 組件可以完成文件的上傳.
-
步驟:
- 在 Jsp 頁面的文件上傳表單里使用 file 標簽. 如果需要一次上傳多個文件, 就必須使用多個 file 標簽, 但它們的名字必須是相同的
-
在 Action 中新添加 3 個和文件上傳相關的屬性. 這 3 個屬性的名字必須是以下格式
- 基本的文件的上傳: 直接在 Action 中定義如下 3 個屬性, 并提供對應的 getter 和 setter
- [File Name] : 類型-File -被上傳的文件。例如:data(fileName要求和文件表單項的name一致)
- [File Name]ContentType : 類型-String -上傳文件的文件類型。例如:dataContentType(用來接收文件類型(MIME值))
- [File Name]FileName : String -上傳文件的文件名。例如:dataFileName (用來接收文件的名字)
-
如果上傳多個文件, 可以使用 List
- 若傳遞多個文件, 則上述的 3 個屬性, 可以改為 List 類型! 多個文件域的 name 屬性值需要一致.
- 示例代碼
1
2
3
4
5
6
7
8
9
|
< s:form action = "testUpload" enctype = "multipart/form-data" > < s:textfield name = "userName[0]" label = "用戶-1" ></ s:textfield > < s:file name = "photos" label = "照片" ></ s:file > < s:textfield name = "userName[1]" label = "用戶-2" ></ s:textfield > < s:file name = "photos" label = "照片" ></ s:file > < s:textfield name = "userName[2]" label = "用戶-3" ></ s:textfield > < s:file name = "photos" label = "照片" ></ s:file > < s:submit value = "提交" ></ s:submit > </ s:form > |
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
|
public class UploadAction extends ActionSupport{ @Setter @Getter private List<File> photos; @Setter @Getter private List<String> photosContentType; @Setter @Getter private List<String> photosFileName; @Setter @Getter private List<String> userName; public String testUpload() throws IOException { System.out.println( "userName: " +userName); System.out.println( "photos: " +photos); System.out.println( "photosFileName: " + photosFileName); System.out.println( "photosContentType: " +photosContentType); // 將文件傳到服務器根目錄下upload文件下 // 獲取ServletContext ServletContext servletContext = ServletActionContext.getServletContext(); //獲取真實路徑 String realPath = servletContext.getRealPath( "/upload" ); System.out.println(realPath); File uploadFile = new File(realPath); //判斷路徑是否存在 if (!uploadFile.exists()){ //不存在創建 uploadFile.mkdir(); } for ( int i = 0 ; i < photos.size(); i++) { UUID uuid = UUID.randomUUID(); FileUtils.copyFile(photos.get(i), new File(realPath + "/" + uuid + photosFileName.get(i))); } return SUCCESS; } } |
1.處理幾個小問題?
1.文件名重名,一般可以在文件名的前面生成一個UUID作為前綴。
2.限制單個文件的大小
3.限制文件的類型
4.限制總文件的大小
2.在Struts2 中提供了 FileUpload 攔截器 可以給我們設置這些屬性值
FileUpload 攔截器有 3 個屬性可以設置.
- maximumSize: 上傳單個文件的最大長度(以字節為單位), 默認值為 2 MB
- allowedTypes: 允許上傳文件的類型, 各類型之間以逗號分隔
- allowedExtensions: 允許上傳文件擴展名, 各擴展名之間以逗號分隔
- 可以在 struts.xml 文件中覆蓋這 3 個屬性
注意: 在 org.apache.struts2 下的 default.properties 中有對上傳的文件總的大小的限制. 可以使用常量的方式來修改該限制struts.multipart.maxSize=2097152
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
|
<constant name= "struts.devMode" value= "true" /> <!-- 在這修改總文件的的大小 --> <constant name= "struts.multipart.maxSize" value= "2097152" /> < package name= "default" namespace= "/" extends = "struts-default" > <interceptors> <interceptor-stack name= "myInterceptor" > <interceptor-ref name= "defaultStack" > <!-- 修改單個文件大小,Commons FileUpload 組件默認接受上傳文件總的最大值為 2M --> <param name= "fileUpload.maximumSize" > 57 , 408 </param> <!-- 允許上傳的文件類型 --> <param name= "fileUpload.allowedTypes" >image/pjpeg,image/gif</param> <!-- 允許上傳文件的擴展名 --> <param name= "fileUpload.allowedExtensions" >jpg,gif</param> </interceptor-ref> </interceptor-stack> </interceptors> < default -interceptor-ref name= "myInterceptor" ></ default -interceptor-ref> <action name= "testUpload" class = "org.pan.action.UploadAction" method= "testUpload" > <result name= "success" >/WEB-INF/views/success.jsp</result> <result name= "input" >/upload.jsp</result> </action> </ package > |
一.上傳文件相關的錯誤消息?
1.與文件上傳有關的出錯消息在 struts-messages.properties 文件里預定義.
2.可以在文件上傳 Action 相對應的資源文件 或者 在 i18n_zh_CN.properties 國際化資源文件中重新定義錯誤消息
1
2
3
4
|
struts.messages.error.file.too.large=你傳的文件太大了 struts.messages.error.content.type.not.allowed=文件類型錯誤 struts.messages.error.file.extension.not.allowed=擴展名錯誤 struts.messages.upload.error.SizeLimitExceededException=文件總大小超過上限 |
文件下載
-
在某些應用程序里, 可能需要動態地把一個文件發送到用戶的瀏覽器中, 而這個文件的名字和存放位置在編程時是無法預知的
-
Stream 結果類型
- Struts 專門為文件下載提供了一種 Stream 結果類型. 在使用一個 Stream 結果時, 不必準備一個 JSP 頁面.
-
Stream 結果類型可以設置如下參數:
- contentType:被下載的文件的 MIME 類型。默認值為 text/plain
- contentLength:被下載的文件的大小,以字節為單位
-
contentDisposition: 可以設置下載文件名的ContentDispositon 響應頭,默認值為 inline,通常設置為如下格式:
- attachment;filename="document.pdf".
- inputName:Action 中提供的文件的輸入流。默認值為 inputStream
- bufferSize:文件下載時緩沖區的大小。默認值為 1024
- allowCaching :文件下載時是否允許使用緩存。默認值為 true
-
contentCharSet:文件下載時的字符編碼。
- 以上參數可以在 Action 中以 getter 方法的方式提供!
- Stream 結果類型的參數可以在 Action 以屬性的方式覆蓋
- 具體使用細節參看 struts-2.3.15.3-all/struts-2.3.15.3/docs/WW/docs/stream-result.html
-
示例代碼
1
|
< a href = "testDownLoad" >下載</ a > |
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
|
public class DownLoadAction extends ActionSupport{ //通常以下這幾個參數會在Action 中提供 @Setter @Getter private String contentType; @Setter @Getter private long contentLength; @Setter @Getter private String contentDisposition; @Setter @Getter private InputStream inputStream; public String testDownLoad() throws FileNotFoundException, UnsupportedEncodingException { //獲取ServletContext ServletContext servletContext = ServletActionContext.getServletContext(); //獲取文件的路徑 String realPath = servletContext.getRealPath( "/WEB-INF/file/至少還有你.mp3" ); //獲取文件的流 inputStream = new FileInputStream(realPath); //設置文件的類型 contentType = servletContext.getMimeType(realPath); //獲取文件的長度 contentLength = new File(realPath).length(); //設置文件名 String fileName = "至少還有你.mp3" ; fileName = new String(fileName.getBytes( "gbk" ), "iso8859-1" ); contentDisposition = "attachment;filename=" +fileName; return SUCCESS; } } |
1
2
3
4
5
6
7
|
<!-- 文件下載 --> < action name = "testDownLoad" class = "org.pan.action.DownLoadAction" method = "testDownLoad" > < result type = "stream" > <!-- 文件緩沖大小 --> < param name = "bufferSize" >2048</ param > </ result > </ action > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/MPPC/p/6133495.html