Struts2上傳單檔案或者多個檔案的好處
問題?Struts2上傳單檔案或者多個檔案的好處在哪裡呢?我們都知道上傳檔案的基本步驟都是讀取檔案域,得到檔案名,然後得到真實存儲路徑,然後再建構輸入輸出流,這樣幾個步驟,我們的檔案上傳也就搭成了。顯得有些麻煩了是吧?接下來讓我們去看看,struts2給我們帶來的便利。
原理:struts2擁有自動轉換類型的功能,這是好處一。struts2我們所拷入的common-io.jar包內建了一個工具類,運用這個工具類可以幫我們達到上傳檔案的目的。
*首先搭建好struts2的環境(7個jar包,攔截器之類的配置),詳情可看:http://blog.csdn.net/mr_li13/article/details/49391329?
案例:運用struts2肯定要用它的配置檔案struts.xml啦、
[html] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration <span style="color:#ff0000;">2.0</span>//EN"
- "http://struts.apache.org/dtds/struts-<span style="color:#ff0000;">2.0</span>.dtd">
- <struts>
- <!-- constant表示常量設定,設定配置檔案自動更新,開發中很重要 -->
- <constant name="struts.action.extension" value="action,,do"></constant>
- <constant name="struts.devMode" value="true"></constant>
- <!-- 各個配置檔案的包含 -->
- <!-- 單個檔案下載下傳action -->
- <include file="UpLoadFile.xml"></include>
- <!-- 多個檔案上傳action -->
- <include file="moreUpLoadFile.xml"></include>
- </struts>
紅色标記處。一定要相同。不然就會出不聯網就會報錯。詳情請看
1.單個檔案:
UpLoadFile.xml檔案内容
[html] view plain copy print ?
- <?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表示常量設定,設定配置檔案自動更新,開發中很重要 -->
- <constant name="struts.action.extension" value="action,,do"></constant>
- <constant name="struts.devMode" value="true"></constant>
- <!-- 設定檔案上傳大小 50M-->
- <constant name="struts.multipart.maxSize" value="52428800"></constant>
- <!-- 檔案下載下傳action -->
- <package name="upload" namespace="/upload" extends="struts-default">
- <action name="upload1" class="com.itcast.web.domain.uploadAction" method="execute">
- <result name="success">/uploadSuccess.jsp</result>
- </action>
- </package>
- </struts>
上傳頁面:upload.jsp
[html] view plain copy print ?
- <!-- 于http://localhost:8080/Struts2day1/upload.jsp -->
- <form action="${pageContext.request.contextPath}/upload/upload1.action" method="post" enctype="multipart/form-data">
- 檔案:<input type="file" name="img"/><br/><!-- name這個名字必須和action類中的setter名字相同,首先執行setter方法,struts2會自動封裝 -->
- <input type="submit" value="上傳"/>
- </form>
action實體類:uploadAction.class
[java] view plain copy print ?
- package com.itcast.web.domain;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Serializable;
- import javax.servlet.ServletContext;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class uploadAction extends ActionSupport implements Serializable {
- private File img;//代表表單中檔案上傳輸入域的名稱,struts會自動封裝成file域在此進行處理
- private String imgFileName;
- //得到上傳域中,檔案的名字,是不是很友善啊,struts已經為我們做好了
- //隻需要與File的對象名稱多FileName就可以了,而且檔案名的編碼格式已經轉化好了
- private String imgContentType;//同理封裝好了一個檔案類型類
- public File getImg() {
- return img;
- }
- public void setImg(File img) {
- this.img = img;
- }
- public String getImgFileName() {
- return imgFileName;
- }
- public void setImgFileName(String imgFileName) {
- this.imgFileName = imgFileName;
- }
- public String getImgContentType() {
- return imgContentType;
- }
- public void setImgContentType(String imgContentType) {
- this.imgContentType = imgContentType;
- }
- public String execute(){
- // 我們自然用最新的方式。簡便許多
- try {
- System.out.println(imgContentType);//image/jpeg
- //找到檔案存放路徑
- ServletContext sc=ServletActionContext.getServletContext();
- String storpath=sc.getRealPath("files");
- //老方式
- //建構輸出流
- // OutputStream out=new FileOutputStream(storpath+"\\"+imgFileName);
- // InputStream in=new FileInputStream(img);
- //
- // byte b[]=new byte[1024];
- // int len=-1;
- // while((len=in.read(b))!=-1){
- // out.write(b, 0, len);
- // }
- // out.close();
- // in.close();
- //新方式,用拷入的jar包commons-io這個jar包,
- //裡面有封裝的一個類,使我們上傳檔案更容易(前者是檔案源,後者是路徑(主路徑,子路徑))
- FileUtils.copyFile(img, new File(storpath, imgFileName));
- ActionContext.getContext().put("message", "上傳成功!");
- return SUCCESS;
- } catch (Exception e) {
- e.printStackTrace();
- return ERROR;
- }
- }
- }
成功頁面:uploadSuccess.jsp
[html] view plain copy print ?
- <body>
- ${message }
- </body>
2.多檔案上傳
其實方式都差不多隻是将實體類中的屬性變成了數組形式
moreUploadFile.xml
[html] view plain copy print ?
- <?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表示常量設定,設定配置檔案自動更新,開發中很重要 -->
- <constant name="struts.action.extension" value="action,,do"></constant>
- <constant name="struts.devMode" value="true"></constant>
- <!-- 設定檔案上傳大小 50M-->
- <constant name="struts.multipart.maxSize" value="52428800"></constant>
- <package name="moreupload" namespace="/moreupload" extends="struts-default">
- <action name="upload1" class="com.itcast.web.domain.moreUploadAction" method="execute">
- <result name="success">/uploadSuccess.jsp</result>
- </action>
- </package>
- </struts>
moreupload.jsp
[html] view plain copy print ?
- <!-- http://localhost:8080/Struts2day1/upload.jsp -->
- <form action="${pageContext.request.contextPath}/moreupload/upload1.action" method="post" enctype="multipart/form-data">
- 檔案1:<input type="file" name="img"/><br/><!-- name這個名字必須和action類中的setter名字相同,首先執行setter方法,struts2會自動封裝 -->
- 檔案2:<input type="file" name="img"/><br/>
- <input type="submit" value="上傳"/>
- </form>
實體類:moreUploadAction.class
[java] view plain copy print ?
- package com.itcast.web.domain;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Serializable;
- import javax.servlet.ServletContext;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class moreUploadAction extends ActionSupport implements Serializable {
- private File[] img;//代表表單中檔案上傳輸入域的名稱,struts會自動封裝成file域在此進行處理
- private String[] imgFileName;
- //得到上傳域中,檔案的名字,是不是很友善啊,struts已經為我們做好了
- //隻需要與File的對象名稱多FileName就可以了,而且檔案名的編碼格式已經轉化好了
- private String[] imgContentType;//同理封裝好了一個檔案類型類
- public File[] getImg() {
- return img;
- }
- public void setImg(File[] img) {
- this.img = img;
- }
- public String[] getImgFileName() {
- return imgFileName;
- }
- public void setImgFileName(String[] imgFileName) {
- this.imgFileName = imgFileName;
- }
- public String[] getImgContentType() {
- return imgContentType;
- }
- public void setImgContentType(String[] imgContentType) {
- this.imgContentType = imgContentType;
- }
- public String execute(){
- try {
- if(img!=null&&img.length>0){
- ServletContext sc=ServletActionContext.getServletContext();
- String storpath=sc.getRealPath("files");
- for(int i=0;i<img.length;i++){
- FileUtils.copyFile(img[i], new File(storpath, imgFileName[i]));
- }
- }
- ActionContext.getContext().put("message", "上傳成功!");
- return SUCCESS;
- } catch (Exception e) {
- e.printStackTrace();
- return ERROR;
- }
- }
- }
成功頁面和uploadSuccess.jso内容一樣的,共用。