(十一)Struts2 檔案上傳

來源:互聯網
上載者:User

標籤:for   commons   ant   contact   位元組   pat   open   fileutils   1.0   

Struts2 架構為依據“基於表單的HTML檔案上傳”所進行的檔案處理上傳提供了內建支援。當檔案上傳時,它通常會儲存在臨時目錄中,然後Action類應對其進行處理或移動到固定目錄中,以確保資料不會丟失。
注意:伺服器可能有適當的安全性原則,禁止你寫入臨時目錄以外的目錄以及屬於Web應用程式的目錄。
通過一個名為FileUpload的預定義攔截器可以在Struts中上傳檔案,該攔截器可通過org.apache.struts2.interceptor.FileUploadInterceptor類獲得,並作為defaultStack的一部分包含在內。你也將在接下來的內容中看到如何使用它在struts.xml檔案中設定各種參數。

 

建立視圖檔案

建立視圖時需要瀏覽和上傳選定的檔案。因此,讓我們先使用HTML上傳表單,建立一個允許使用者上傳檔案的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>File Upload</title></head><body>   <form action="upload" method="post" enctype="multipart/form-data">      <label for="myFile">Upload your file</label>      <input type="file" name="myFile" />      <input type="submit" value="Upload"/>   </form></body></html>

  

 

在上面的例子中有幾點值得注意。首先,表單的enctype設定為multipart/form-data,要使得檔案上傳攔截器成功處理檔案上傳,這個就必須設定。然後要注意的是表單的action方法上傳和檔案上傳欄位的名稱(即myFile)。我們需要這些資訊來建立action方法和struts配置。
接下來讓我們建立一個簡單的jsp檔案success.jsp來顯示我們檔案上傳成功後的結果。

 

<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>File Upload Success</title></head><body>You have successfully uploaded <s:property value="myFileFileName"/></body></html>

  

以下是結果檔案error.jsp,一旦上傳檔案出錯時會使用:

<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>File Upload Error</title></head><body>There has been an error in uploading the file.</body></html>

  

建立Action類

接下來,讓我們建立一個名為uploadFile.java的Java類,它將負責上傳檔案並將檔案儲存體在安全的位置:

package cn.w3cschool.struts2;import java.io.File;import org.apache.commons.io.FileUtils;import java.io.IOException; import com.opensymphony.xwork2.ActionSupport;public class uploadFile extends ActionSupport{   private File myFile;   private String myFileContentType;   private String myFileFileName;   private String destPath;   public String execute()   {      /* Copy file to a safe location */      destPath = "C:/apache-tomcat-6.0.33/work/";      try{      System.out.println("Src File name: " + myFile);      System.out.println("Dst File name: " + myFileFileName);                File destFile  = new File(destPath, myFileFileName);     FileUtils.copyFile(myFile, destFile);        }catch(IOException e){         e.printStackTrace();         return ERROR;      }      return SUCCESS;   }   public File getMyFile() {      return myFile;   }   public void setMyFile(File myFile) {      this.myFile = myFile;   }   public String getMyFileContentType() {      return myFileContentType;   }   public void setMyFileContentType(String myFileContentType) {      this.myFileContentType = myFileContentType;   }   public String getMyFileFileName() {      return myFileFileName;   }   public void setMyFileFileName(String myFileFileName) {      this.myFileFileName = myFileFileName;   }}

  

uploadFile.java是一個非常簡單的類。要注意的重點是,FileUpload攔截器和Parameters攔截器為我們承擔了所有的重工作量。預設情況下,FileUpload攔截器為你提供三個參數,它們分別按以下方式命名:

  • [檔案名稱參數] - 這是使用者已上傳的實際檔案。在這個例子中它將是“myFile”

  • [檔案名稱參數]ContentType - 這是上傳的檔案的內容類型。在這個例子中,它將是“myFileContentType”

  • [檔案名稱參數]FileName - 這是上傳的檔案的名稱。在這個例子中,它將是“myFileFileName”

得益於Struts攔截器這三個參數均可供我們使用。我們要做的是在Action類中建立三個帶有正確名稱的參數,並使這些變數可以自動連接。所以,在上面的例子中,我們有三個參數和一個action方法。如果一切正常,則返回“success”,否則返回“error”。

設定檔

以下是控制檔案上傳過程的Struts2 配置屬性:

序號 屬性和說明
1 struts.multipart.maxSize

可接受的上傳檔案的最大值(以位元組為單位),預設值為250M。

2 struts.multipart.parser

用於上傳多部分表單的庫,預設為jakarta

3 struts.multipart.saveDir

儲存臨時檔案的位置,預設是javax.servlet.context.tempdir。

你可以在應用程式的struts.xml檔案中使用constant標籤更改任意一個這些設定,我們可以看以下struts.xml的樣本:

<?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>   <constant name="struts.devMode" value="true" />   <constant name="struts.multipart.maxSize" value="1000000" />   <package name="helloworld" extends="struts-default">   <action name="upload" class="cn.w3cschool.struts2.uploadFile">       <result name="success">/success.jsp</result>       <result name="error">/error.jsp</result>   </action>   </package></struts>

  

因為FileUpload攔截器是defaultStack攔截器的一部分,我們不需要準確的配置它,但你可以在<action>中添加<interceptor-ref>標籤。fileUpload攔截器有兩個參數:maximumSizeallowedTypes。 maximumSize參數是設定所允許的檔案大小的最大值(預設約為2MB)。allowedTypes參數是所允許的內容(MIME)類型的用逗號分隔的列表,如下所示:

<action name="upload" class="cn.w3cschool.struts2.uploadFile">       <interceptor-ref name="basicStack">       <interceptor-ref name="fileUpload">           <param name="allowedTypes">image/jpeg,image/gif</param>       </interceptor-ref>       <result name="success">/success.jsp</result>       <result name="error">/error.jsp</result>   </action>

  

以下是web.xml檔案的內容:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   id="WebApp_ID" version="3.0">      <display-name>Struts 2</display-name>   <welcome-file-list>      <welcome-file>index.jsp</welcome-file>   </welcome-file-list>   <filter>      <filter-name>struts2</filter-name>      <filter-class>         org.apache.struts2.dispatcher.FilterDispatcher      </filter-class>   </filter>   <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</url-pattern>   </filter-mapping></web-app>

  

現在按右鍵項目名稱,然後單擊“Export”>“WAR File”建立WAR檔案。然後在Tomcat的webapps目錄中部署WAR檔案。最後,啟動Tomcat伺服器並嘗試訪問URL http://localhost:8080/HelloWorldStruts2/upload.jsp,將顯示如下介面:

現在使用瀏覽按鈕選擇一個檔案“Contacts.txt”,然後點擊上傳按鈕,上傳檔案到伺服器,你將看到如下頁面。你上傳的檔案應該儲存在C:\apache-tomcat-6.0.33\work下。

注意:FileUpload攔截器會自動刪除上傳的檔案,因此你必須在上傳的檔案被刪除之前將其以編程方式儲存在某個位置。

錯誤資訊

fileUplaod攔截器使用幾個預設的錯誤資訊key:

序號 錯誤資訊key和說明
1 struts.messages.error.uploading

無法上傳檔案時發生的常規錯誤。

2 struts.messages.error.file.too.large

當上傳的檔案過大(由maximumSize指定)時發生。

3 struts.messages.error.content.type.not.allowed

當上傳的檔案與指定的預期內容類型不匹配時發生。

你可以在WebContent/WEB-INF/classes/messages.properties資源檔中覆蓋這些訊息文本。

(十一)Struts2 檔案上傳

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.