上傳文件是不少Web程序都具備的功能。Struts2自己沒有提供解析上傳文件內容的功能,它使用第三方的文件上傳組件提供對文件上傳的支持。因此咱們要想利用Struts2實現文件上傳的功能,首先要將commons-fileupload-1.2.1.jar和commons-io-1.4.jar複製到項目的WEB-INF/lib目錄下。
咱們知道,Struts1.x的上傳組件須要一個ActionForm來輔助傳遞文件,而Struts2的上傳組件卻很簡單,只用一個攔截器:org.apache.struts2.interceptor.FileUploadInterceptor(這個攔截器不用配置,是自動裝載的),它負責調用底層的文件上傳組件解析文件內容,併爲Action準備與上傳文件相關的屬性值。這裏要強調的是:處理文件上傳請求的Action必須提供特殊樣式命名的屬性。例如,假設表單中文件選擇框的名字爲upload,那麼Action就應該提供如下三個屬性upload,uploadFileName,uploadContentType來分別表示上傳文件的File對象、上傳文件名以及上傳文件內容類型。不少人由於忽略了這一點而犯錯誤。
下面是上傳單個文件的JSP頁面代碼singleUpload.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上傳單個文件</title>
</head>
<body>
<s:actionerror />
<s:form action="upload" method="post" enctype="multipart/form-data">
<s:file name="upload" label="文件名" />
<s:textfield name="description" label="文件描述" />
<s:submit value="上傳" />
</s:form>
</body>
</html>
注意粗體部分的設置,這是有上傳控件的表單所要求的格式。下面是用於上傳的動做類的完整代碼:
package org.leno.struts2.action;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
// 表明上傳文件的File對象
private File upload;
// 上傳文件名
private String uploadFileName;
// 上傳文件的MIME類型
private String uploadContentType;
// 上傳文件的描述信息
private String description;
// 保存上傳文件的目錄,相對於WEB應用程序的根路徑,在struts.xml中配置
private String uploadDir;
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 getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUploadDir() {
return uploadDir;
}
public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}
@Override
public String execute() throws Exception {
String newFileName = null;
// 獲得當前時間自1970年1月1日0時0分0秒開始走過的毫秒數
long now = System.currentTimeMillis();
// 獲得保存上傳文件的目錄的真實路徑
File dir = new File(ServletActionContext.getServletContext()
.getRealPath(uploadDir));
// 若是該目錄不存在,就建立
if (!dir.exists()) {
dir.mkdirs();
}
// 爲避免重名文件覆蓋,判斷上傳文件是否有擴展名,以時間戳做爲新的文件名
int index = uploadFileName.lastIndexOf(".");
if (index != -1) {
newFileName = now + uploadFileName.substring(index);
} else {
newFileName = Long.toString(now);
}
// 讀取保存在臨時目錄下的上傳文件,寫入到新的文件中
InputStream is = new FileInputStream(upload);
OutputStream os = new FileOutputStream(new File(dir, newFileName));
byte[] buf = new byte[1024];
int len = -1;
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
is.close();
os.close();
return SUCCESS;
}
}
在execute方法中的實現代碼就很簡單了,只是從臨時文件複製到指定的路徑(在這裏是web應用程序下的uploadDir目錄)中。上傳文件的臨時目錄的默認值是javax.servlet.context.tempdir的值,但能夠經過struts.properties(和struts.xml在同一個目錄下)的struts.multipart.saveDir屬性設置。Struts2上傳文件的默認大小限制是2M(2097152字節),也能夠經過struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=102400 表示一次上傳文件的總大小不能超過100K字節。另外一種改變上傳屬性的方式是在struts.xml中配置constant。本文采用後者。
下面是咱們要用到的Struts2的核心配置文件struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<struts>
<!-- 上傳文件的臨時目錄 -->
<constant name="struts.multipart.saveDir" value="e:\\temp"></constant>
<!-- 上傳文件的總大小限制 -->
<constant name="struts.multipart.maxSize" value="102400"></constant>
<!-- 資源文件配置 -->
<constant name="struts.custom.i18n.resources"
value="ApplicationResources">
</constant>
<package name="default" extends="struts-default">
<action name="upload"
class="org.leno.struts2.action.UploadAction">
<!—文件上傳攔截器 -->
<interceptor-ref name="defaultStack">
<!-- 設置Action能接受的文件的最大長度,而不是對上傳文件的最大長度進行限制。
(由於在Action處理以前,文件已經上傳到服務器了。) -->
<param name="fileUpload.maximumSize">102400</param>
<param name="fileUpload.allowedTypes">
p_w_picpath/gif,p_w_picpath/jpeg,p_w_picpath/pjpeg
</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/singleUpload.jsp</result>
<param name="uploadDir">/WEB-INF/UploadFiles</param>
</action>
</package>
</struts>
當咱們對文件上傳進行了更多的控制,上傳的文件不知足所指定的限制條件時,咱們可使用特定的I18N鍵添加相關的錯誤消息。在src下新建ApplicationResources.properties:
struts.messages.error.uploading=文件上傳錯誤
struts.messages.error.file.too.large=文件上傳長度超過了限制的長度
struts.messages.error.content.type.not.allowed=不允許上傳這種類型的文件
這樣,上傳文件若是出錯,框架去會自動導向到input結果頁面,同時顯示錯誤信息;若是成功,就能夠導航到success.jsp。咱們能夠在success.jsp頁中經過<s:property>得到文件的屬性(文件名,文件內容類型,文件描述以及文件的長度),代碼以下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上傳成功</title>
</head>
<body>
<h1>上傳成功,文件信息以下:</h1>
文件名:<s:property value="uploadFileName" /><br/>
文件大小:<s:property value="upload.length()" /><br/>
文件類型:<s:property value="uploadContentType" /><br/>
文件描述:<s:property value="description" /><br/>
</body>
</html>