Use Struts2 to upload and download files

Source: Internet
Author: User
Tags constant file upload

1. Implement file upload in Struts2
1. Add necessary jar packages

Commons-logging-1.1.jar
Freemarker-2.3.8.jar
Ognl-2.6.11.jar
Struts2-core-2.0.6.jar
Xwork-2.0.1.jar
Commons-io-1.3.1.jar
Commons-fileupload-1.2.jar

2. jsp page settings

1) the form submission method must be post.
2) the form label must add the attribute enctype = "multipart/form-data"
3) add the name attribute to the form element file.

3. Write the Action Class

1) declare three corresponding attributes based on the name attribute of the form element file. One is File type and the other two are strings.
Type. The attribute names are fixed. They are used to store uploaded files, file names, and file types.

Private file name attribute;
Private String name attribute name FileName;
Private String name attribute name ConentType;
And provides the corresponding setter/getter method.

2) write code in the execute method

The code is as follows: Copy code

String path = ServletActionContext. getServletContext (). getRealPath ("/") + "/files/" + headerFileName;
FileUtils. copyFile (header, new File (path); </p>
<P style = "padding-left: 30px;">

3) if multiple files are uploaded at a time, make sure that the names of all the upload controls are the same.
Collection or array to receive information about all uploaded files, and operate the file set or array in the execute method.

The code is as follows: Copy code

// Determine the directory to Upload
For (int I = 0; I <source. size (); I ++ ){
If (source. get (I )! = Null & source. get (I). exists ()){
File dest = new File (path + sourceFileName. get (I ));
FileUtils. copyFile (source. get (I), dest );
    }
}

4. struts. xml configuration file
1) Note that the action used for upload must reference the fileupload blocker. The blocker is included in the screenshot stack by default.

If the user-defined blocker is referenced, the blocker is invalid by default;
2) you can configure some constant values to control the size of the uploaded files.

The code is as follows: Copy code

<Constant name = "struts. multipart. maxSize" value = "1024000"> </constant>

II. Download Struts2 real files
1. Add necessary jar packages

Commons-logging-1.1.jar
Freemarker-2.3.8.jar
Ognl-2.6.11.jar
Struts2-core-2.0.6.jar
Xwork-2.0.1.jar
Commons-io-1.3.1.jar
Commons-fileupload-1.2.jar

2. jsp page settings

The code is as follows: Copy code

<A href = "download. action? Filename={{' .doc '} "> permission requirement document </a>
<A href = "download. action? Filename={'wind.jpg '} "> landscape painting </a>

Provide a link for download, submit it to action upon clicking, and provide the name and address of the file to be downloaded
2. Write the Action Class

1) create an attribute, receive the submitted path name and file name, and provide the getter/setter method.
2) exectue method can return directly without writing any code
3) compile the download method

The code is as follows: Copy code

// Actual download method: the method name is the get encapsulation of the attribute value of inputName in the result parameter
Public InputStream getDownload () throws Exception {
// 1. Obtain the complete path of the object to be downloaded.
String path = ServletActionContext. getServletContext (). getRealPath ("/") + "/files/" + name;
// 2. You must obtain the HttpServletReponse object.
HttpServletResponse response = ServletActionContext. getResponse ();
// 3. Set the response content type
Response. setContentType ("application/x-msdownload"); // specifies that the response action is download.
// 4. Convert the file to be downloaded into a stream
FileInputStream stream = new FileInputStream (new File (path ));
// 5. What is displayed in the pop-up download box?
// Response. setHeader ("Content-Disposition", "attachment; filename =" + name );
Response. setHeader ("Content-Disposition", "attachment; filename =" + new String (name. getBytes

("Gbk"), "iso-8859-1 "));
Return stream;
    }

Note that this method is customized. Note that the method name must comply with the camper naming method and start with get.

3. struts. xml configuration file

The code is as follows: Copy code

<Action name = "download" class = "cn. mystruts. action. DownloadAction">
<Result name = "OK" type = "stream">
<Param name = "inputName"> downloadFile </param>
<Param name = "bufferSize"> 1024 </param>
</Result>
</Action>

Note: The value given by the inputName parameter is the name of the compiled download method, which is removed from get and must comply with the hump naming method.

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.