In the web development, we often have the file upload download function, the previous introduction we explained the upload function, then this time we talk about the download function .
1. First we need to have a test page for processing the download, which is the JSP.
<body>
2. We only need to write the simplest test code, then we need to have a request to receive the action,(need to note the file name in Chinese garbled processing)
Package Com.babybus.sdteam.action;import Java.io.inputstream;import Org.apache.struts2.ServletActionContext; Import com.opensymphony.xwork2.actionsupport;//File Download public class Filedownloadaction extends Actionsupport{private int Number;p rivate String filename;public int getnumber () {return number;} public void Setnumber (int number) {this.number = number;} Public String GetFileName () {return fileName;} public void Setfilename (String fileName) {this.filename = FileName;} Returns an input stream, which is an input stream as a client, but for the server side is an output stream public InputStream getdownloadfile () throws Exception{this.filename = " Address Book. xls ";//solution garbled This.filename = new String (this.fileName.getBytes (" GBK ")," iso-8859-1 "); return Servletactioncontext.getservletcontext (). getResourceAsStream ("upload/Address Book. xls");} @Overridepublic String Execute () throws Exception {return SUCCESS;}}
3. Finally, we need to configure a struts.xml. Note the following
1) The result type must be written as type= "stream" , and the corresponding processing class is Org.apache.struts2.dispatcher.StreamResult
2) <param name= "contentdisposition">attachment;filename= "${filename}" </param> Contentdisposition default is inline(inline), for example, the downloaded file is text type , directly on the Web page open , can not open directly Open the Download box and choose it yourself
3)attachment: Download box will open when downloading
4) filename= "${filename}": In this definition the name is a dynamic , the name is the file name that is displayed on the download box
5) <param name= "InputName" >downloadFile</param>, this downloadFile name is to be getDownloadFile() method names in the Filedownloadaction.java class remove Get
<action name= "FileDownload" class= "com.babybus.sdteam.action.FileDownloadAction" > <result name= " Success "type=" Stream "> <param name=" ContentType ">text/plain</param> <param name=" Contentdisposition ">attachment;filename=" ${filename} "</param> <param name=" InputName "> downloadfile</param> <param name= "buffersize" >1024</param> </result></action >
After the configuration is complete, we implement the Struts2 Simple file download function.
Conclusion
- Benefited and mastered the Struts2 download file
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4718695.html
[Javaweb Basics] 010.struts2 Download file