Struts2默認(rèn)是使用了Common-FileUpload的文件上傳框架,并對(duì)其進(jìn)行封裝,簡(jiǎn)化,使用時(shí)相當(dāng)方便。
首先寫(xiě)一張簡(jiǎn)單的上傳頁(yè)面:upload.html
<html>
<head>
<title>file upload</titel>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="upload.action">
file:<input type="file" name="upload"/><br>
<input name="dd" type="submit" value="submit"/>
</form>
</body>
</html>
<head>
<title>file upload</titel>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="upload.action">
file:<input type="file" name="upload"/><br>
<input name="dd" type="submit" value="submit"/>
</form>
</body>
</html>
配置處理的Action:UploadAction.java
其中必須提供3個(gè)屬性,
類型為File的xxx屬性來(lái)封裝文件信息
類型為String 的xxxFileName屬性來(lái)封裝文件域?qū)?yīng)文件的文件名
類型為Stri ng的xxxContentType屬性來(lái)封裝文件域中對(duì)應(yīng)文件的文件類型
在下面的代碼 中還定義了一個(gè)savePath屬性,依賴注入方式,通過(guò)配置文件里來(lái)動(dòng)態(tài)修改屬性的值
package my;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
/**
*
*/
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath()
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setUpload(File upload)
{
this.upload = upload;
}
public File getUpload()
{
return this.upload;
}
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String getUploadContentType()
{
return this.uploadContentType;
}
public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}
public String getUploadFileName()
{
return this.uploadFileName;
}
@Override
public String execute()throws Exception
{
FileOutputStream fos = new FileOutputStream(getSavePath() + "http://" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}
}
配置Action:struts.xmlimport java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
/**
*
*/
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath()
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setUpload(File upload)
{
this.upload = upload;
}
public File getUpload()
{
return this.upload;
}
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String getUploadContentType()
{
return this.uploadContentType;
}
public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}
public String getUploadFileName()
{
return this.uploadFileName;
}
@Override
public String execute()throws Exception
{
FileOutputStream fos = new FileOutputStream(getSavePath() + "http://" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}
}
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name ="my" extends="struts-default">
<action name="upload" class="my.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
</package>
</struts>
配置web.xml文件:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
處理結(jié)果頁(yè)面:succ.jspxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>upload success</title>
</head>
<body>
<s:property value="title"/>
<br>
<img src="<s:property value="'upload/'+uploadFileName"/>"/> <br>
</body>
</html>
這樣實(shí)現(xiàn)了簡(jiǎn)單的文件上傳功能.<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>upload success</title>
</head>
<body>
<s:property value="title"/>
<br>
<img src="<s:property value="'upload/'+uploadFileName"/>"/> <br>
</body>
</html>
在實(shí)際應(yīng)用時(shí)一般還需要文件過(guò)濾功能:
Struts2內(nèi)置了一個(gè)文件上傳攔截器fileUpload,通過(guò)配置此攔截器輕松實(shí)現(xiàn)文件過(guò)濾.
在配置過(guò)程時(shí),可以指定兩個(gè)參數(shù):
alledTypes:指定允許上傳的文件類型,多個(gè) 文件類型之間以英文逗號(hào)隔開(kāi)
maximumSize:指定允許上傳的文件大小。
當(dāng)文件過(guò)濾失敗后,系統(tǒng)自動(dòng)轉(zhuǎn)入input邏輯視圖,所以在Action里需要配置input視圖,另外還必須地為Action配置defaultStack的攔截器引用:
<?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>
<package name ="my" extends="struts-default">
<action name="upload" class="my.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
</package>
</struts>
在input視圖中通過(guò)<s:fielderror/>來(lái)顯示錯(cuò)誤信息,此信息是英文,配置國(guó)際化來(lái)顯示中文信息(如下):<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name ="my" extends="struts-default">
<action name="upload" class="my.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</action>
</package>
</struts>
#更改上傳文件類型不允許的提示信息
struts.messages.error.content.type.not.allowed=文件上傳失?。耗阋蟼鞯奈募愋筒辉试S
#更改上傳文件太大的提示信息
struts.messages.error.file.too.large=文件上傳失敗:你要上傳的文件太大
#文件上傳其它錯(cuò)誤信息
struts.messages.error.uploading=文件上傳失?。喊l(fā)生內(nèi)部錯(cuò)誤