Struts2默認是使用了Common-FileUpload的文件上傳框架,并對其進行封裝,簡化,使用時相當方便。
首先寫一張簡單的上傳頁面:
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>
配置處理的Action:
UploadAction.java
其中必須提供3個屬性,
類型為File的xxx屬性來封裝文件信息
類型為String
的xxxFileName屬性來封裝文件域對應文件的文件名
類型為Stri
ng的xxxContentType屬性來封裝文件域中對應文件的文件類型
在下面的代碼
中還定義了一個savePath屬性,依賴注入方式,通過配置文件里來動態修改屬性的值
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.xml
<!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>
處理結果頁面:succ.jsp
<%@ 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>
這樣實現了簡單的文件上傳功能.
在實際應用時一般還需要文件過濾功能:
Struts2內置了一個文件上傳攔截器fileUpload,通過配置此攔截器輕松實現文件過濾.
在配置過程時,可以指定兩個參數:
alledTypes:指定允許上傳的文件類型,多個 文件類型之間以英文逗號隔開
maximumSize:指定允許上傳的文件大小。
當文件過濾失敗后,系統自動轉入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視圖中通過<s:fielderror/>來顯示錯誤信息,此信息是英文,配置國際化來顯示中文信息(如下):
#更改上傳文件類型不允許的提示信息
struts.messages.error.content.type.not.allowed=文件上傳失敗:你要上傳的文件類型不允許
#更改上傳文件太大的提示信息
struts.messages.error.file.too.large=文件上傳失敗:你要上傳的文件太大
#文件上傳其它錯誤信息
struts.messages.error.uploading=文件上傳失敗:發生內部錯誤