基於Struts2的單檔案和多檔案上傳

來源:互聯網
上載者:User

標籤:sage   int   2.0   無法   ognl   eem   類型   開發模式   package   

Struts2 已經有預設的upload攔截器。我們只需要寫參數,它就會自動幫我們封裝好。

一、 單檔案上傳

1.1 導jar包

commons-fileupload-1.3.jar

commons-io-2.0.1.jar

commons-logging-1.1.3.jar

freemarker-2.3.19.jar

ognl-3.0.6.jar

struts2-core-2.3.15.3.jar

xwork-core-2.3.15.3.jar

1.2 web.xml配置Struts2核心過濾器

  1   <filter>  2     <filter-name>struts2</filter-name>  3     <filter-class>  4          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  5     </filter-class>  6   </filter>  7   8   <filter-mapping>  9     <filter-name>struts2</filter-name> 10     <url-pattern>/*</url-pattern> 11   </filter-mapping>
XML Code

1.3 JSP檔案上傳頁(注意file的name屬性一定要與Action裡面的File類型屬性名稱一致)

  1     <!-- 檔案上傳的必要前提: 
  2       1.請求方式必須是post
  3       2.enctype屬性的取值必須是multipart/form-data 
  4       3.提供一個檔案選擇域 檔案上傳  5       -->  6   7       <s:form  action="upload.action" enctype="multipart/form-data">  8  9             <s:file  name="myfile"  label="請選擇檔案"></s:file> 10             <s:submit value  ="提交"></s:submit> 11  12       </s:form>
Jsp Code

1.4 實現檔案上傳的UploadAction

  1 package com.fhcq.action;  2   3 import java.io.File;  4 import javax.servlet.ServletContext;  5 import org.apache.commons.io.FileUtils;  6 import org.apache.struts2.ServletActionContext;  7 import com.opensymphony.xwork2.ActionSupport;  8   9 public class UploadAction extends ActionSupport  { 10  11 //表單上提供的欄位 12 private File myfile; 13 //struts2在檔案上傳時提供的屬性 14 private String myfileFileName;//上傳的檔案名稱。上傳欄位名稱+FileName  注意大小寫 15 private String myfileContentType;//上傳檔案的MIME類型。上傳欄位名稱+ContentType 注意大小寫 16 
 17      public String upload (){ 18  19 //1.拿到ServletContext 20 ServletContext servletContext = ServletActionContext.getServletContext(); 21 //2.調用realPath方法,擷取根據一個虛擬目錄得到的真實目錄 22 String realPath = servletContext.getRealPath("/WEB-INF/file"); 23//3.如果這個真實的目錄不存在,需要建立 24 File file = new File(realPath ); 25 if(!file.exists()){ 26 file.mkdirs(); 27 } 28 //4.把檔案存過去 29 //拷貝:把檔案的臨時檔案複製到指定的位置。注意:臨時檔案還在 30 //FileUtils.copyFile(myfile, new File(file,myfileFileName)); 31  32 //剪下:把臨時檔案剪下指定的位置,並且給他重新命名。 注意:臨時檔案沒有了 33 myfile.renameTo(new File(file,myfileFileName)); 34 return SUCCESS; 35 } 36    37 public File getMyfile() { 38 return myfile; 39 } 40  41 public void setMyfile(File myfile) { 42 this.myfile = myfile; 43 } 44  45 public String getMyfileFileName() { 46 return myfileFileName; 47 } 48  49 public void setMyfileFileName(String myfileFileName) { 50 this.myfileFileName = myfileFileName; 51 } 52  53 public String getMyfileContentType() { 54 return myfileContentType; 55 } 56   57 public void setMyfileContentType(String myfileContentType) { 58 this.myfileContentType = myfileContentType; 59 } 60  61 } 62 
Action Code

1.5 配置struts.xml(注意:修改檔案上傳的總的大小。)

  1 <struts>  2      <!--constant修改struts2的預設配置值  -->  3      <!--修改struct2稱為開發模式,修改配置之後會自動載入  -->  4      <constant name="struts.devMode" value="true" />  5      <!-- 限制上傳檔案的大小,第一種方式:通過修改struts2的常量。  6 在default.properties中配的struts.multipart.maxSize=2097152-->  7      <constant name="struts.multipart.maxSize" value="4097152" />  8   9      <package name="p1"  extends="struts-default"> 10  11   <action  name ="upload" class = "com.fhcq.action.UploadAction" method="upload"> 12  13           <!-- 第二種方式:通過給fileUpload的攔截器注入參數的形式,控制上傳檔案的大小 14           無法使用   <param name="fileUpload.maximumSize">31457280</param>--> 15      <interceptor-ref name="defaultStack"> 16 <!-- 限制上傳檔案的副檔名。如果有多個,使用逗號分隔 17 <param name="fileUpload.allowedExtensions">jpg,png,bmp</param>--> 18 <!-- 限制上傳檔案的MIME類型。 如果有多個,使用逗號分隔 --> 19                <param name="fileUpload.allowedTypes">image/jpg,image/pjpeg,image/png</param> 20      </interceptor-ref> 21      <result>/index.jsp</result> 22      <result name="input" type="redirect">/upload.jsp</result> 23   </action> 24  25       </package> 26  27 </struts> 28 
XML Code

1.6 配置fileupload_message.properties,放置在src下用來顯示錯誤的資源檔資訊

  1 struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u6587\u4EF6\u7684\u7C7B\u578B\u4E0D\u662F\u88AB\u652F\u6301\u7684: {0} "{1}" "{2}" {3}
Properties Code

 

二、 多檔案上傳

在以上單檔案上傳的基礎上,修改內容如下:

2.1 JSP檔案上傳頁(注意file的name屬性一定要與Action裡面的File類型屬性名稱一致)

  1     <!-- 檔案上傳的必要前提:  2    1.請求方式必須是post  3    2.enctype屬性的取值必須是multipart/form-data  4    3.提供一個檔案選擇域  5       -->  6   7       <s:form  action="upload.action" enctype="multipart/form-data">  8   9             <s:file  name="myfile"  label="請選擇檔案"></s:file> 10             <s:file  name="myfile"  label="請選擇檔案"></s:file> 11             <s:submit value  ="提交"></s:submit> 12  13       </s:form>
Jsp Code

2.2 實現檔案上傳的UploadAction

  1 package com.fhcq.action;  2   3 import java.io.File;  4   5 import javax.servlet.ServletContext;  6   7 import org.apache.struts2.ServletActionContext;  8   9 import com.opensymphony.xwork2.ActionSupport; 10  11 public class UploadAction extends ActionSupport  { 12  13 //表單上提供的欄位 14 private File[]   myfile; 15 //struts2在檔案上傳時提供的屬性 16 private String[] myfileFileName;上傳的檔案名稱。上傳欄位名稱+FileName  注意大小寫 17 private String[] myfileContentType;//上傳檔案的MIME類型。上傳欄位名稱+ContentType 注意大小寫 18  19 public String upload (){ 20  21 //1.拿到ServletContext 22 ServletContext servletContext = ServletActionContext.getServletContext(); 23 //2.調用realPath方法,擷取根據一個虛擬目錄得到的真實目錄 24 String realPath = servletContext.getRealPath("/WEB-INF/files"); 25 //3.如果這個真實的目錄不存在,需要建立 26 File file = new File(realPath ); 27 if(!file.exists()){ 28 file.mkdirs(); 29 } 30 //4.把檔案存過去 31 //剪下:把臨時檔案剪下指定的位置,並且給他重新命名。 注意:臨時檔案沒有了 32 for(int i=0;i<myfile.length;i++){ 33 myfile[i].renameTo(new File(file,myfileFileName[i])); 34 } 35  36 return SUCCESS; 37 } 38  39 public File[] getMyfile() { 40 return myfile; 41 } 42  43 public void setMyfile(File[] myfile) { 44 this.myfile = myfile; 45 } 46  47 public String[] getMyfileFileName() { 48 return myfileFileName; 49 } 50  51 public void setMyfileFileName(String[] myfileFileName) { 52 this.myfileFileName = myfileFileName; 53 } 54  55 public String[] getMyfileContentType() { 56 return myfileContentType; 57 } 58  59 public void setMyfileContentType(String[] myfileContentType) { 60 this.myfileContentType = myfileContentType; 61 } 62  63 } 64 
Action Code

基於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.