Struts file Download, 2 ways:
Method 1: Write the byte stream data to the browser through the response object, and set the download response header.
Way 2:struts Way
Struts File Download:
1, Core: in the Struts-defautl
<result-type name= "Stream" class= "Org.apache.struts2.dispatcher.StreamResult"/>
2, the implementation of the Code:
1) Project directory structure:
2) list.jsp
3) Downaction.java
Package sram.fileupload;
Import Java.io.File;
Import Java.io.InputStream;
Import java.io.UnsupportedEncodingException;
Import Java.net.URLEncoder;
Import Java.util.Map;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
/** * File Download * 1. Displays the list of all files to be downloaded * 2. File Download */public class Downaction extends actionsupport{/** * 1. Display a list of all files to download */public string list () throws exception{//Get upload directory path, String path = Servletactioncontext.getse
Rvletcontext (). Getrealpath ("/upload");
directory Object File File = new file (path);
Get the file name of all files to be downloaded string[] fileNames = File.list ();
Save Actioncontext AC = Actioncontext.getcontext ();
Map//This form is also OK for the Ac.getcontextmap () that represents the request;
map<string,object> request = (map<string, object>) ac.get ("request");
Request.put ("FileNames", fileNames); Return "LIst ";
}/** * 2. File download *///2.1. Gets the file name of the files to download private String filename; public void Setfilename (String filename) {//mouse is moved to download, the file name is displayed using the Get method submission, so to solve the Chinese garbled problem try {Filen
ame = new String (filename.getbytes ("iso8859-1"), "UTF-8");
} catch (Unsupportedencodingexception e) {throw new RuntimeException (e);
}//processed file name, assignment this.filename = fileName;
//2.2. Download the submitted business method (configure the return stream in Struts.xml) public String down () throws exception{return "Download"; }//2.3. The method of returning a file stream//getattrinputstream is obviously a getter method, so Attrinputstream is a property public InputStream Getattrinputstrea
M () {return Servletactioncontext.getservletcontext (). getResourceAsStream ("/upload/" +filename); }//2.4. Download the displayed file name (the file name displayed by the browser) public String getdownfilename () {//need to be encoded in Chinese try {filename
= Urlencoder.encode (FileName, "UTF-8"); } catch (Unsupportedencodingexception E) {throw new RuntimeException (e);
} return fileName; }
}
4) Struts.xml configuration
<package name= "FileUpload" namespace= "/" extends= "Struts-default" > <action name= "down_*" class= " Sram.fileupload.DownAction "method=" {1} > <!--1. List--<result name= "list" >/fileupload/ List.jsp</result> <!--2. Download-<result name= "Download" type= "stream" > <!-- Call the method that returns the stream in action-<!--The file types that are allowed to be downloaded: Specify here for all binary file types find exe corresponding <mime-type in Conf/web.xml of Tomacat > <param name= "contentType" >application/octet-stream</param> <!-- The property in the corresponding action: the property that returns the stream "is actually the Attrinputstream in Getattrinputstream ()"--<param name= "InputName" >attrinp
utstream</param> <!--download header, including: Browser display filename Filename=${downfilename} This is not an El expression, The EL expression can only be used with JSP, which is the value symbol for struts in the configuration file--<param name= "Contentdisposition" >attachment;fil
Ename=${downfilename}</param> <!--buffer size set--<param name= "buffersize" >1024</param> </result> </action> </package>
3. Note: execution process
1) JSP page-->downaction.java
2) Downaction.java-->struts.xml
Struts File upload download code has uploaded resources.