struts2 File Upload Download principle

Source: Internet
Author: User
Tags file upload

File Upload:

STRUTS2 is actually implemented in two steps when uploading a file:
1) First save the file uploaded by the client to the directory specified by the Struts.multipart.saveDir key, and if the directory for that key does not exist, it will be saved to the directory specified by the JAVAX.SERVLET.CONTEXT.TEMPDIR environment variable.
2) The member variable file of the file type defined in action actually points to a temporary file in the temporary directory, and then writes the temporary file to the specified server-side directory on the server side via IO.

Where was the Struts.multipart.saveDir specified? In the default.properties of Org.apache.struts2, specify the following:

struts.multipart.savedir=
struts.multipart.maxsize=

Upload the file with Struts2 as follows:

public class Uploadaction extends Actionsupport
{
Private String username;

Private file file;//When the process is transferred to the action, file already points to the real file.

Private String Filefilename;

Private String filecontenttype;//Note that the naming of the last two member variables is regular.
}
File uploads are also intercepted by interceptor, fileupload this interceptor. and has been added to the default interceptor stack. Some parameters can be set by this interceptor. such as the maximum number of bytes uploaded, etc., but does not work, Configure it with a similar configuration in the default.properties.
The configuration of the Struts2 constant configuration. such as:, with struts.properties inside the disposition of who priority? is struts.properties high. But the actual development is either in the Struts.properties match, It is the same thing to do in Struts.xml.

File Download:

STRUTS2 Download File Implementation instructions

ContentType

Content type, consistent with the specified type in the Internet MIME standard, such as text/plain for plain text, text/xml for GIF images, and image/jpeg for JPG images

InputName

The source stream of the downloaded file corresponds to a property name of type InputStream in the action class, such as a property with a value of InputStream that requires the getInputStream () method to be written

Contentdisposition

File download processing methods, including inline (inline) and attachment (attachment), and attachment mode will pop up the File Save dialog box, otherwise the browser will try to display the file directly. Values are:

Attachment;filename= "Struts2.txt" means that the name of the file to be saved should be struts2.txt when it is downloaded. If the direct write filename= "Struts2.txt", then the default is to represent inline, the browser will try to automatically open it, equivalent to the wording: inline; Filename= "Struts2.txt"

BufferSize

Size of the download buffer

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.