struts2 File Upload Download

Source: Internet
Author: User

File Upload

Introduction to File Upload in 1.struts2

Struts2 file upload requires the use of the file upload components (Commons-fileupload.jar and Commons-io.jar) provided by Apache.

The core of the Struts2 file upload is implemented by the FileUpload interceptor.

2. How to implement File upload

1> add commons-fileupload and Commons-io packages

2> On the JSP page, do the following configuration

Set the method property value of the form to post

Add a property enctype= "Multipart/form-data" to the form tag (let the submitted form data be submitted as a stream)

3> Add the following attribute to the action class and add the Get|set method

Private File xxx;

Private String Xxxfilename;

Private String Xxxcontenttype;

Note: XXX refers to the name value of the "File" > File field of the <input type=

Struts2 File Upload function, is to save the client upload files to a temporary directory, we have to do is to save the temporary directory files in the specified directory.

File download

How to implement file download in Struts2


1. Add a file to download the action class

public class downloadaction{

Private InputStream Logostream;

Public InputStream Getlogostream () {

try{

This.filename = Brand.getlogourl ();

Gets the absolute path to the file being downloaded
String FilePath = Servletactioncontext.getrequest (). Getservletcontext (). Getrealpath (Brand.getlogourl ());

System.out.println (FilePath);

Read the downloaded file
Logostream = new FileInputStream (FilePath);

return logostream;

}catch (Exception ex) {

Ex.printstacktrace ();
return null;
}
}
}

2. Add an action method to the class to return the success

public class downloadaction{

Public String Download () {

Return "Success";

}

}

3. Configure the action in Struts.xml, and result type is stream

<action name= "Download" class= "... Downloadaction "method=" Download ">
<result name= "Success" type= "Stream" >

</result>
</action>

4. In result, set the parameter InputName, the value of which must be a property of the action class with a data type of inputstream.

<action name= "Download" class= "... Downloadaction "method=" Download ">
<result name= "Success" type= "Stream" >
<param name= "InputName" >logoStream</param>
</result>
</action>

5. Control the file name of the downloaded file: Add parameter in Result: Contentdisposition


<action name= "Download" class= "... Downloadaction "method=" Download ">
<result name= "Success" type= "Stream" >
<param name= "InputName" >logoStream</param>
<param name= "contentdisposition" >attachment;filename= "${filename}" </param>
</result>
</action>

Note: FileName in ${filename} is a property in the action class

6. Control file Type: Add parameter to result: ContentType

ContentType:

Word:application/msword

Excel:application/vnd.ms-excel

Ppt:application/vnd.ms-powerpoint

Html:text/html

Text file: Text/plain

Executable file: Application/octet-stream


<action name= "Download" class= "... Downloadaction "method=" Download ">
<result name= "Success" type= "Stream" >
<param name= "InputName" >logoStream</param>
<param name= "contentdisposition" >attachment;filename= "${filename}" </param>
<param name= "ContentType" >application/octet-stream</param>
</result>
</action>

struts2 File Upload 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.