一、Struts2文件上傳
Struts2的文件上傳實現非常簡單,只需要簡單幾步就可完成;
注意:
(1)文件上傳的struts2標簽為:<s:file name="" label="上傳"/>
(2)文件上傳的前提是表單屬性method="post" enctype="multipart/form-data";
(3)web應用中必須包含common-fileupload.jar和common-io.jar,因為struts2默認上傳解析器使用的是jakarta;
(4)可以在struts.xml中配置最大允許上傳的文件大小:<constant name="struts.multipart.maxSize" value="....."/>,默認為2M;
1.普通文件上傳
實現規則:
(1)在JSP中設定表單控件<s:file name="upload" label="上傳"/>
(2)在Action中定義屬性:
private File upload; //包含文件內容
private String uploadFileName; //上傳文件的名稱;
private String uploadContentType; //上傳文件的MIME類型;
這些屬性都會隨著文件的上傳自動賦值;
(3)在execute()中完成寫入磁盤功能;
代碼示例:
Upload01Action.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
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
|
package org.upload.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class Upload01Action extends ActionSupport { private File upload; private String uploadFileName; private String uploadContentType; private String name; public String execute() throws Exception{ String path = ServletActionContext.getServletContext().getRealPath( "/WEB-INF/upload" ); String filename = path+File.separator+name; FileInputStream in = new FileInputStream(upload); FileOutputStream out = new FileOutputStream(filename); byte []b = new byte [ 1024 ]; int len = 0 ; while ((len=in.read(b))> 0 ){ out.write(b, 0 ,len); } out.close(); return SUCCESS; } public File getUpload() { return upload; } public void setUpload(File upload) { this .upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this .uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this .uploadContentType = uploadContentType; } public String getName() { return name; } public void setName(String name) { this .name = name; } } |
struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts > < constant name = "struts.devMode" value = "true" /> < constant name = "struts.custom.i18n.resources" value = "message" ></ constant > < package name = "default" namespace = "/" extends = "struts-default" > < action name = "upload0*" class = "org.upload.action.Upload0{1}Action" > < param name = "name" >1.jpg</ param > < result >/{1}.jsp</ result > </ action > </ package > </ struts > |
1.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html > < head > < title >My JSP '1.jsp' starting page</ title > </ head > < body > < s:form action = "upload01" method = "post" enctype = "multipart/form-data" > < s:file name = "upload" label = "上傳" ></ s:file > < s:submit value = "上傳" ></ s:submit > </ s:form > </ body > </ html > |
2.利用攔截器進行過濾
手動實現過濾的方式非常簡單,就是利用輸入校驗的方式進行過濾,即在validate()中進行過濾;
而這里要講的攔截器方式是很好的方式,只需要在配置文件中配置,靈活性很好,能夠限制文件的類型、文件的大??;如果上傳的文件不符合要求,則返回input邏輯視圖;
配置攔截器步驟:
(1)文件上傳的攔截器為fileUpload;
(2)需要給定參數allowedTypes、maximumSize;
(3)在fileUpload攔截器后,需要添加<interceptor-ref name="defaultStack"/>
代碼示例:
由于通過攔截器進行過濾只需要配置struts.xml,因此這里只給出struts.xml的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts > < constant name = "struts.devMode" value = "true" /> < constant name = "struts.custom.i18n.resources" value = "message" ></ constant > < package name = "default" namespace = "/" extends = "struts-default" > < action name = "upload0*" class = "org.upload.action.Upload0{1}Action" > < interceptor-ref name = "fileUpload" > < param name = "allowedTypes" >image/jpeg,image/gif</ param > < param name = "maximumSize" >1024*1024</ param > </ interceptor-ref > < interceptor-ref name = "defaultStack" ></ interceptor-ref > < param name = "name" >1.jpg</ param > < result >/{1}.jsp</ result > < result name = "input" >/{1}.jsp</ result > </ action > </ package > </ struts > |
我們還需要配置文件上傳失敗后的錯誤提示信息,我們需要在全局國際化資源文件中配置:
1
2
3
|
struts.messages.error.content.type.not.allowed=文件類型不匹配 struts.messages.error.file.too.large=文件太大 |
二、Struts2文件下載
我們在學習Servlet和HTTP協議時已經可以實現文件下載,即寫content-disposition頭即可,struts2的實現原理也是這個,但是提供了更好的封裝性;
struts2的stream結果類型專門用于實現文件下載;
(1)struts.xml中配置stream結果類型,并配置contentType、contentDisposition、bufferSize參數即可,模板:
1
2
3
4
5
6
7
|
< action name = "download" class = "" > < result type = "stream" name = "success" > < param name = "contentType" ></ param > < param name = "contentDisposition" >attachment;filename=""</ param > < param name = "bufferSize" >4096</ param > </ result > </ action > |
(2)在Action中創建public InputStream getInputStream()throws Exception;方法,此方法用于獲得下載文件的輸入流;
DownloadAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package org.download.action; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport { public InputStream getInputStream() throws Exception{ return ServletActionContext.getServletContext().getResourceAsStream( "/WEB-INF/upload/1.jpg" ); } public String execute() throws Exception{ return SUCCESS; } } |
struts.xml
1
2
3
4
5
6
7
|
< action name = "download" class = "org.download.action.DownloadAction" > < result type = "stream" name = "success" > < param name = "contentType" >image/jpeg</ param > < param name = "contentDisposition" >attachment;filename="1.jpg"</ param > < param name = "bufferSize" >4096</ param > </ result > </ action > |
就可以完成下載。
java中struts2文件上傳下載的實現就為大家介紹到這里,謝謝大家的閱讀。