struts-Basic content -8-File download

Source: Internet
Author: User

Struts2 file download requires action to provide a way to return the InputStream stream, which represents the entry for the downloaded file.

One: Write Action class

Downloadaction.java

 PackageAction;ImportCom.opensymphony.xwork2.ActionSupport;ImportJava.io.FileInputStream;ImportJava.io.InputStream;/*** Created by Cxspace on 16-7-12.*/ Public classDownloadactionextendsactionsupport{//This property value can be specified dynamically in the configuration file, and the path to the file to be downloaded    PrivateString InputPath;
PublicString Getinputpath () {returnInputPath; } //method of Dependency Injection changing attribute value Public voidSetinputpath (String inputpath) { This. InputPath =InputPath; }

/x
Define a method to return InputStream, which will act as the entry for the downloaded file, and need to configure the InputName parameter specified by the stream type result
InputName parameter value is the method to remove the get prefix, the first letter lowercase string
x/ PublicInputStream Gettargetfile ()throwsException {return NewFileInputStream (InputPath); }}

Two: Configure action

Configuring the desired key download, the key is to configure a result of type stream

Four key attributes to be specified in the result

ContentType: Specifies the file type of the downloaded file

InputName: Specifies the ingress input stream for the downloaded file

Contentdispostion: Specifies the file name of the download

BufferSize: Specifies the buffer size when downloading a file

 PackageAction;

ImportCom.opensymphony.xwork2.ActionSupport;
ImportJava.io.FileInputStream;
ImportJava.io.InputStream;

/**
* Created by Cxspace on 16-7-12.
*/
Public classDownloadactionextendsactionsupport{

//This property value can be specified dynamically in the configuration file
PrivateStringInputPath;

PublicStringGetinputpath() {
returnInputPath;
}

//method of Dependency Injection changing attribute value
Public voidSetinputpath(String InputPath) {
This.InputPath= InputPath;
}

PublicInputStreamGettargetfile()throwsException {

return newFileInputStream (InputPath);
}
}

struts-Basic content -8-File download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.