文件上傳功能在Struts2中獲得了很好的封裝,主要使用fileUpload上傳組件。html
uploadTest1.jspjava
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page isELIgnored="false"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <s:form action="uploadT1" method="post" enctype="multipart/form-data"> <s:file name="uploadFile"></s:file> <br /> <s:submit value="提交"></s:submit> </s:form> </body> </html>
注意:代碼中加入了enctype="multipart/form-data";並且使用了<s:file/>標籤,尤爲注意<s:file/>的name屬性。apache
showResult1.jsp數組
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page isELIgnored="false"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> 您上傳的文件名爲:<s:property value="uploadFileFileName" />,存放目錄:<s:property value="#request.storeDir"/> <br /> <br/> <!--下載上下文路徑--> <s:a href="%{#request.get('javax.servlet.forward.context_path')}/upload/%{uploadFileFileName}">點擊這裏下載</s:a> <br /> </body> </html>
package com.clzhang.struts2.demo12; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadTest1Action extends ActionSupport { public static final long serialVersionUID = 1; private File uploadFile; private String uploadFileFileName; public File getUploadFile() { return uploadFile; } public void setUploadFile(File uploadFile) { this.uploadFile = uploadFile; } public String getUploadFileFileName() { return uploadFileFileName; } public void setUploadFileFileName(String uploadFileFileName) { this.uploadFileFileName = uploadFileFileName; } public String execute() throws IOException { // 上傳物理路徑 String targetDirectory = ServletActionContext.getServletContext().getRealPath("/upload"); // 生成上傳的File對象 File target = new File(targetDirectory, uploadFileFileName); // 複製File對象,從而實現上傳文件 FileUtils.copyFile(uploadFile, target); ServletActionContext.getRequest().setAttribute("storeDir", targetDirectory); return SUCCESS; } }
Action實例中File變量命名規則:服務器
struts.multipart.maxSize=2048000000 struts.multipart.saveDir=/upload
一般此文件存放於WEB-INF\classes目錄下。jsp
<action name="uploadT1" class="com.clzhang.struts2.demo12.UploadTest1Action"> <result>/struts2/demo12/showResult1.jsp</result> </action>
打開IE,輸入地址:http://127.0.0.1:8080/st/struts2/demo12/uploadTest1.jspide
結果以下:post
選擇任意一個文件提交後,結果以下:測試
一個,顯示了服務器物理存儲地址;二個,提供了下載連接。this
Struts2支持多文件批量上傳,將上面的代碼中的File類型改爲File[]便可。
uploadTest2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page isELIgnored="false"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <s:form action="uploadT2" method="post" enctype="multipart/form-data"> username:<s:textfield name="fileUploadTools.username"></s:textfield> <br /> <s:file name="fileUploadTools.uploadFile"></s:file> <br /> <s:file name="fileUploadTools.uploadFile"></s:file> <br /> <s:file name="fileUploadTools.uploadFile"></s:file> <br /> <s:submit value="提交"></s:submit> </s:form> </body> </html>
注意:由於是屬性驅動,因此表單元素的命名須要以fileUploadTools打頭。
showResult2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page isELIgnored="false"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> 您上傳的文件名列表: <br/> <!--對Action中對象(fileUploadTools)包含的數組(uploadFileFileName)的遍歷--> <s:iterator value="fileUploadTools.uploadFileFileName" status="st"> <s:iterator value="fileUploadTools.uploadFileFileName[#st.index]"> <s:property /> <br/> </s:iterator> </s:iterator> <br /> </body> </html>
UploadTest2Action.java
package com.clzhang.struts2.demo12; import java.io.IOException; import com.opensymphony.xwork2.ActionSupport; public class UploadTest2Action extends ActionSupport { public static final long serialVersionUID = 1; // 聲明封裝了File上傳的FileUploadTools類的實例 // FileUploadTools類也封裝了上傳的屬性及get和set方法 private FileUploadTools fileUploadTools = new FileUploadTools(); public FileUploadTools getFileUploadTools() { return fileUploadTools; } public void setFileUploadTools(FileUploadTools fileUploadTools) { this.fileUploadTools = fileUploadTools; } @Override public String execute() throws IOException { fileUploadTools.beginUpload(); return SUCCESS; } }
FileUploadTools.java
package com.clzhang.struts2.demo12; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; public class FileUploadTools { private String username; private File uploadFile[];// 上傳的文件是數組類型 private String uploadFileFileName[];// 文件名是數組類型 private String uploadFileContentType[]; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } // 上傳的ContentType文件類型也是數組類型 // 必需要加上對ContentType的聲明,不然會出現異常 public String[] getUploadFileContentType() { return uploadFileContentType; } public void setUploadFileContentType(String[] uploadFileContentType) { this.uploadFileContentType = uploadFileContentType; } public File[] getUploadFile() { return uploadFile; } public void setUploadFile(File[] uploadFile) { this.uploadFile = uploadFile; } public String[] getUploadFileFileName() { return uploadFileFileName; } public void setUploadFileFileName(String[] uploadFileFileName) { this.uploadFileFileName = uploadFileFileName; } public String beginUpload() throws IOException { System.out.println("用戶名:" + username); String targetDirectory = ServletActionContext.getServletContext().getRealPath("/upload"); for (int i = 0; i < uploadFile.length; i++) { File target = new File(targetDirectory, new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss") .format(new Date()).toString() + System.nanoTime() + uploadFileFileName[i]); FileUtils.copyFile(uploadFile[i], target); } return "success"; } }
<action name="uploadT2" class="com.clzhang.struts2.demo12.UploadTest2Action"> <result>/struts2/demo12/showResult2.jsp</result> </action>
打開IE,輸入地址:http://127.0.0.1:8080/st/struts2/demo12/uploadTest2.jsp
結果以下:
任意選擇三個文件後,提交,結果以下: