Struts2實現單檔案的上傳功能例項詳解及原始碼
阿新 • • 發佈:2019-01-05
Struts2完成檔案的上傳功能例項
10級學員 郞志課堂筆記
在這裡通過一個例項簡單寫一下struts2實現檔案上傳的流程。
首先通過手寫的方式寫一個上傳的流程
第一步:建立相應的jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div align="center"> <h1>檔案上傳</h1> <!-- 此處的enctype屬性必須要更改,一般此處不需要更改,預設為application/x-www-form-urlencoded,它只處理表單裡的value屬性傳遞的是字串,但是當上傳檔案時必須要改為:multipart/form-data --> <form action="${pageContext.request.contextPath }/upFile.action" enctype="multipart/form-data" method="post"> 上傳檔案:<input type="file" name="upload"/><br/> <input type="submit" value="上傳">" </form> </div> </body> </html>
注意:
Form表單的Enctype屬性有以下三個值:
1) application/x-www-form-urlencoded:這是預設編碼方式,它只處理表單域裡的value屬性值,採用這種編碼方式的表單會將表單域的值處理成URL編碼方式。
2) multipart/form-data:這種編碼方式的表單會以二進位制流的方式來處理表單資料,這種編碼方式會把檔案域指定檔案的內容也封裝到請求引數裡。(經常用)
3) text/plain:這種方式主要適用於直接通過表單傳送郵件的方式。
檔案上傳是web應用經常用到的一個知識。原理是,通過為表單元素設定enctype=”multipart/form-data”屬性,讓表單提交的資料以二進位制編碼的方式提交,在接收此請求的Servlet中用二進位制流來獲取內容,就可以取得上傳檔案的內容,從而實現檔案的上傳。
頁面效果:
第二步:編寫對應的action檔案
package cn.csdn.hr.up.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport{ private static final long serialVersionUID = 1L; // 以下封裝的引數是固定的,為:檔名 檔名+ContentType 檔名+FileName //上傳檔案 private File upload; //上傳檔案的型別 private String uploadContentType; //上傳檔案的名稱 private String uploadFileName; public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } @Override public String execute() throws Exception { //宣告檔案上傳的路徑 String path = ServletActionContext.getServletContext().getRealPath("/upload"); //根據路徑名建立一個檔案 File file = new File(path); //判斷file是否存在,如果不存在,則自動建立一個 if(!file.exists()){ file.mkdirs(); } //通過呼叫copyFile()方法將指定的檔案copy到指定的路徑中 FileUtils.copyFile(upload, new File(file, uploadFileName)); return SUCCESS; } }
第三步:在struts2配置檔案中定義UploadAction檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="upload" extends="struts-default">
<action name="upFile" class="cn.csdn.hr.up.action.UploadAction">
<!-- 上傳成功後跳轉到success.jsp頁面 -->
<result name="success">/sucess.jsp</result>
<!-- 上傳失敗後返回到upload.jsp頁面 -->
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
這裡就完成了手動上傳檔案的功能,可以通過Tomcat等伺服器測試一下了。成功後會跳轉到success.jsp頁面:
顯示檔案上傳成功。
上面是為了讓大家更加了解檔案上傳的內部處理過程,其實struts2已經通過攔截器封裝好了檔案上傳的功能,我認為封裝的還是非常完整的。
上面實現的功能完全可以用以下攔截器去實現:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--
因為在struts.properties檔案中指明瞭struts檔案上傳的預設大小為2M <constant
name="struts.multipart.maxSize" value="1024*1024*2"></constant>
-->
<package name="upload" extends="struts-default">
<!-- 通過全域性定義當上傳出錯時返回到update.jsp頁面 -->
<global-results>
<result name="input">/update.jsp</result>
</global-results>
<action name="upload" class="cn.csdn.upload.action.UploadAction">
<interceptor-ref name="fileUpload">
<!-- 限制上傳檔案的格式 -->
<param name="allowedTypes">image/JPEG,image/bmp,image/pjpeg,image/gif</param>
<!--
新增一個攔截器的時候雖然在這裡設定了檔案上傳大小的限制,但是由於
在struts.properties檔案中有struts檔案的設定所以在上面必須新增常量設定
-->
<param name="maximumSize">10485760</param>
</interceptor-ref>
<!-- 當新增一個攔截器的時候,那麼預設的攔截器必須手動新增 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 依賴注入屬性 -->
<!-- 設定上傳檔案的路徑 -->
<param name="savePath">/upload</param>
<!-- 檔案上傳成功後跳轉到success.jsp頁面 -->
<result name="success">/test.jsp</result>
</action>
</package>
</struts>