Mode 1:
Query data: Struts 2 realizes the file download function_ tanyunlong_nice column - CSDN blog_ Struts 2 download function
About result type = "stream" in struts 2_ Z69183787 column - CSDN blog
. jsp code
<a href="javascript:openxieyiApplication()">Template download</a>
function openxieyiApplication() { window.open("test.action?fileName=model_1.docx"); }
. xml code
<action name="test" class="openFileInfoAction" method="fileDownLoad"> <result name="success" type="stream"> <param name="contentType">application/octet-stream;charset=ISO8859-1</param> <param name="inputName">fileStream</param> <param name="contentDisposition">attachment;filename=${fileName}</param> <param name="bufferSize">1024</param> </result> </action>
. java code
package com.iss.fs.basic.action; import java.io.InputStream; import org.apache.struts2.ServletActionContext; public class OpenFileInfoAction extends BaseAction{ //The file name of the downloaded template private String fileName=""; //Input stream private InputStream fileStream; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getFileStream() { return fileStream; } public void setFileStream(InputStream fileStream) { this.fileStream = fileStream; } public String fileDownLoad() throws FileNotFoundException { fileName= getRequest().getParameter("fileName");//Get file name (fileName corresponds to ${fileName} of xml file configuration) fileStream = OpenFileInfoAction.class.getResourceAsStream("template/"+fileName);//Get the relative path of the file (fileStream corresponds to the fileStream with name="inputName" of the xml file configuration) return "success"; } }
Note:
1. The fileName must have a getFileName() method to correspond to the ${fileName} value configured in the xml file
2.fileStream must have getFileStream() method to correspond to the value of [fileStream] in < param name = "inputname" > fileStream < / param > configured in xml file
3.OpenFileInfoAction.class.getResourceAsStream can only find the template file in the subfolder of the current class openfileinfoaction or the template file in the root directory, so the location of the template file will be limited
Mode 2:
Query data: Struts 2 realizes the file download function pdf originality document
xml configuration has more path paths than "method 1", which can broaden the location of template files
. jsp code
<a href="javascript:openxieyiApplication()">Template download</a>
function openxieyiApplication() { window.open("test.action?fileName=model_1.docx"); }
. xml code
<action name="test" class="openFileInfoAction" method="fileDownLoad"> <param name="path">/WEB-INF/classes/com/iss/fs/basic/template/model_1.docx</param> <result name="success" type="stream"> <param name="contentType">application/octet-stream;charset=ISO8859-1</param> <param name="inputName">fileStream</param> <param name="contentDisposition">attachment;filename=${fileName}</param> <param name="bufferSize">1024</param> </result> </action>
. java code
package com.iss.fs.basic.action; import java.io.InputStream; import org.apache.struts2.ServletActionContext; public class OpenFileInfoAction extends BaseAction{ //The file name of the downloaded template private String fileName=""; //Input stream private InputStream fileStream; //Path to download template file private String path=""; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getFileStream() { return fileStream; } public void setFileStream(InputStream fileStream) { this.fileStream = fileStream; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String fileDownLoad() throws FileNotFoundException { try { fileName = new String(getRequest().getParameter("fileName").getBytes(), "ISO8859-1");//Get file name fileStream = ServletActionContext.getServletContext().getResourceAsStream(path); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "success"; }
Summary:
1.type="stream": output general content to stream stream
2.contentType: Specifies the file type of the downloaded file (content type, which is consistent with the type specified in the Internet MIME standard. For example, text/plain stands for plain text, text/xml stands for XML, image/gif stands for GIF pictures, image/jpeg stands for JPG pictures, and application / octet stream is unlimited)
3.inputName: Specifies the name of the input stream. For example, if fileStream is written here, it will automatically find getFileStream() defined in action. Therefore, there must be a getFileStream() method in the class, or declare the fileStream variable to get the value assigned by the get()set() method under appropriate circumstances. The essence of the two methods is the same.
4.contentDisposition :
Attachment: indicates the attachment mode, that is, the download dialog box pops up when downloading. If you don't write, the default is inline, that is, the browser will try to open the download instead of the download dialog box
filename="${fileName}", the getFileName() method in the Action class will be called, so there must be getFileStream() in the class. Or declare the filename variable to get the value assigned by the get()set() method under appropriate circumstances. The essence of the two methods is the same.
5.bufferSize the buffer size of the downloaded file
6. The contenttype attribute and contentDisposition correspond to the content type and content disposition headers in the HTTP response respectively.