Struts2 providesStreamResult type. The result type supports the file download function. When specifying the stream result type, you must specifyInputnameParameter. This parameter specifies an input stream, which is the entry to the downloaded file. The file downloading support of struts2 allows the system to control the permission of the viewer to download files, including downloading files with non-western European characters.
Index. jsp:
<% @ Page contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML>
Struts. xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts> <package name="demo" extends="struts-default"> <action name="download" class="action.DownloadAction"> <param name="fileName">vi.jpg</param> <param name="fileDir">/uploadDir</param> <result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">targetFile</param> <param name="contentDisposition">attachment;filename=${fileName}</param> </result> </action> </package></struts>
The action for configuring the downloaded file is not much different from that for configuring common actions. The key is to configure a result of the stream type. The result of the stream type will be downloaded as a response. To configure the stream type, you must specify the following four attributes:
① Contenttype: Specifies the file type of the downloaded object.
② Inputname: Specifies the input stream of the object to be downloaded.
③ Contentdisposition: Specifies the name of the downloaded file and the method in which the file is downloaded. Attachment indicates that the file is downloaded as an attachment.
④ Buffersize: Specifies the buffer size for downloading files.
The logic view of the stream result type returns an input stream to the client, so you do not need to specify the location attribute.
Downloadaction. Java:
Public class downloadaction extends actionsupport {private string filename; private string filedir; private inputstream targetfile; Public String getfilename () {return filename;} public void setfilename (string filename) {This. filename = filename;} Public String getfiledir () {return filedir;} public void setfiledir (string filedir) {This. filedir = filedir;} public inputstream gettargetfile () {system. out. println ("Enter the gettargetfile method"); Return targetfile;} public void settargetfile (inputstream targetfile) {system. out. println ("Enter the settargetfile method" using repeated this.tar GetFile = targetfile;} Public String execute () {system. out. println ("Enter the execute method... "); servletcontext context = servletactioncontext. getservletcontext (); string filepath = context. getrealpath (getfiledir () + file. separator + getfilename (); try {targetfile = new fileinputstream (filepath); Return "success";} catch (filenotfoundexception e) {e. printstacktrace (); Return "fail ";}}}