分享知識-快樂自己:Struts2檔案上傳及檔案下載
阿新 • • 發佈:2018-11-12
1)Struts2單檔案上傳
action:類檔案
package com.mlq.action; import com.opensymphony.xwork2.ActionSupport; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import java.io.File; import java.io.IOException; /** * Struts2:單檔案上傳 * * @author asus */ public classUploadFileAction extends ActionSupport { //臨時檔案 private File upload; //檔案型別(底層規定屬性名稱) private String uoloadContextType; //檔名稱(底層規定屬性名稱) private String uploadFileName; //檔案存放路徑 private String savePath; @Override public void validate() { System.out.println("嚴重方法"); }//預設訪問方法 @Override public String execute() throws IOException { //檔案全路徑 File destFile = new File(ServletActionContext.getRequest().getRealPath(savePath) + "\\" + getUploadFileName()); String path = destFile.getPath(); System.out.println(path + "------"); FileUtils.copyFile(upload, destFile);return "success"; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUoloadContextType() { return uoloadContextType; } public void setUoloadContextType(String uoloadContextType) { this.uoloadContextType = uoloadContextType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } }
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> <package name="user" namespace="/user" extends="struts-default"> <!--全域性配置資訊--> <global-results> <result name="input">/upload.jsp</result> </global-results> <!--檔案上傳--> <action name="upload" class="com.mlq.action.UploadFileAction"> <!--內建設定action類中的屬性值--> <param name="savePath">/upload</param> <result>/upload.jsp</result> <!--設定檔案引數--> <interceptor-ref name="fileUpload"> <param name="maximumSize">512000</param> <param name="allowedType">image/jpg</param> </interceptor-ref> <!--預設的攔截器--> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
upload.jsp:JSP頁面
<%@ taglib prefix="s" uri="/struts-tags" %> <%-- Created by IntelliJ IDEA. User: asus Date: 2018/10/4 Time: 10:32 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>StrutsTow檔案上傳</title> </head> <body> <div> <fieldset> <legend>Struts2檔案上傳</legend> <form action="/user/upload" enctype="multipart/form-data" method="post"> 選擇檔案:<input type="file" name="upload"> <input type="submit" value="提交"> </form> </fieldset> <br/><br/> <fieldset> <legend>上傳的圖片回顯</legend> <img width="200px" height="300px" src="/upload/<s:property value='uploadFileName'/>"/> </fieldset> <br/><br/> <fieldset> <legend>錯誤資訊</legend> <s:fielderror></s:fielderror> </fieldset> </div> </body> </html>
Web.xml:核心配置
<!--核心控制器--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>upload.jsp</welcome-file> </welcome-file-list>
2)Struts2多檔案上傳
action:類檔案
package com.mlq.action; import com.opensymphony.xwork2.ActionSupport; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import java.io.File; import java.io.IOException; /** * Struts2:單檔案上傳 * * @author asus */ public class UploadsFileAction extends ActionSupport { //臨時檔案 private File[] upload; //檔案型別 private String[] uoloadContextType; //檔名稱 private String[] uploadFileName; //檔案存放路徑 private String savePath; @Override public void validate() { System.out.println("嚴重方法"); } //預設訪問方法 @Override public String execute() throws IOException { for (int i = 0; i < upload.length; i++) { File temp=upload[i]; //檔案全路徑 File destFile = new File(ServletActionContext.getRequest().getRealPath(savePath) + "\\" + uploadFileName[i]); String path = destFile.getPath(); System.out.println(path + "------"); FileUtils.copyFile(temp, destFile); } return "success"; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUoloadContextType() { return uoloadContextType; } public void setUoloadContextType(String[] uoloadContextType) { this.uoloadContextType = uoloadContextType; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } }
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.multipart.maxSize" value="5097152"/> <package name="user" namespace="/user" extends="struts-default"> <!--全域性配置資訊--> <global-results> <result name="input">/upload.jsp</result> </global-results> <!--檔案上傳--> <action name="upload" class="com.mlq.action.UploadsFileAction"> <!--內建設定action類中的屬性值--> <param name="savePath">/upload</param> <result>/upload.jsp</result> <!--設定檔案引數--> <interceptor-ref name="fileUpload"> <param name="maximumSize">512000</param> <param name="allowedType">image/jpg</param> </interceptor-ref> <!--預設的攔截器--> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts>
upload.jsp:JSP頁面
<%@ taglib prefix="s" uri="/struts-tags" %> <%-- Created by IntelliJ IDEA. User: asus Date: 2018/10/4 Time: 10:32 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>StrutsTow檔案上傳</title> </head> <body> <div> <fieldset> <legend>Struts2檔案上傳</legend> <form action="/user/upload" enctype="multipart/form-data" method="post"> 選擇檔案:<input type="file" name="upload"> <br/> 選擇檔案:<input type="file" name="upload"> <br/> 選擇檔案:<input type="file" name="upload"> <input type="submit" value="提交"> </form> </fieldset> <br/><br/> <fieldset> <legend>上傳的圖片回顯</legend> <s:iterator value="uploadFileName"> <img width="200px" height="300px" src="/upload/<s:property/>"/> </s:iterator> </fieldset> <br/><br/> <fieldset> <legend>錯誤資訊</legend> <s:fielderror></s:fielderror> </fieldset> </div> </body> </html>
web.xml:核心配置
<!--核心控制器--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>upload.jsp</welcome-file> </welcome-file-list>
3)Struts2檔案下載
action:類檔案
package com.mlq.action; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; /** * Struts2:檔案下載 * * @author asus */ public class FileDownAction extends ActionSupport { /*** * 讀取下載檔案的目錄 */ private String inputPath; /*** * 下載檔案的檔名 */ private String fileName; /*** * 讀取下載檔案的輸入流 */ private InputStream inputStream; public String getInputPath() { return inputPath; } public void setInputPath(String inputPath) { this.inputPath = inputPath; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getInputStream() throws FileNotFoundException { String realPath = ServletActionContext.getServletContext().getRealPath(inputPath); System.out.println(realPath+"\\"+fileName); return new BufferedInputStream(new FileInputStream(realPath+"\\"+fileName)); } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } }
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> <package name="user" namespace="/user" extends="struts-default"> <!--檔案上傳--> <action name="downLoad" class="com.mlq.action.FileDownAction"> <!--指定檔案下載目錄地址--> <param name="inputPath">/upload</param> <!--設定下載型別--> <result name="success" type="stream"> <!--設定傳送到瀏覽器的MIMe型別--> <param name="contentType">application/octet-stream</param> <!--設定輸入流的名稱--> <param name="inputStream">inputStream</param> <!--提示使用者開啟還是下載--> <param name="contentDisposition">attachment;filename="${fileName}"</param> <!--緩衝區的大小:沒有嚴格要求可隨意設定--> <param name="bufferSize">4096</param> </result> </action> </package> </struts>
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%-- Created by IntelliJ IDEA. User: asus Date: 2018/10/4 Time: 10:32 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>StrutsTow檔案下載</title> </head> <body> <div> <fieldset> <legend>Struts2檔案下載</legend> <a href="/user/downLoad?fileName=t01a4036c8714c169fd.jpg">檔案下載</a> </fieldset> </div> </body> </html>
web.xml核心配置
<!--核心控制器--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>