Upload and download struts2 files

Source: Internet
Author: User

I. File Upload:

The constant struts. multipart. parser indicates the technology used by struts2 to upload files. The default value is Jakarta. If the COs framework is used, you only need to import the package and change the constant to Cos.

1. Upload a single file:

A. Form method = "Post" enctype = "multipart/form-Data", there is a file form item name = xxx

B. Write several attributes in action.

Private file xxx; // The xxx attribute is used to encapsulate the uploaded file.

Private string xxxcontenttype; // attribute used to encapsulate the type of the uploaded file

Private string xxxfilename; // used to encapsulate the file name to be uploaded

Provide getter and setter

C. You only need to dump the file in the execution method corresponding to the action.

D. Set the file size and type. You need to set parameters for the interceptor of the uploaded file in the action, and add <Interceptor-ref name = "fileupload">
<! -- The unit is byte. Here it is 400 K. The size here refers to the size of each file to be uploaded. If multiple files are uploaded, it refers to the size of each file. -->
<Param name = "maximumsize"> 409600 </param>
<! -- Specifies the File Upload type. All file types are available in Tomcat 6.0 \ conf \ WEB. xml. -->
<! -- Word type, separated by "," -->
<Param name = "allowedtypes">
Application/MSWord
</Param>
</Interceptor-ref>
<! -- Of course, the default interceptor must be added. -->
<Interceptor-ref name = "defaultstack"> </Interceptor-ref>

If the file upload verification fails, the i18n key is as follows:

Struts. Messages. Error. Uploading-common error messages that cannot be uploaded to a file
Struts. Messages. Error. file. Too. Large-the error message returned when the length of the uploaded file is too large.
Struts. Messages. Error. content. type. Not. Allowed-when the uploaded file does not meet the specified contenttype

<S: fielderror name = "XXX"> </S: fielderror>

2. Multifile upload:

You only need to modify the attribute in the preceding action to an array:

Private file [] xxx; // The xxx attribute is used to encapsulate the uploaded file.

Private string [] xxxcontenttype; // attribute used to encapsulate the type of the uploaded file

Private string [] xxxfilename; // used to encapsulate the file name to be uploaded

 

Ii. File Download

1. In Action

Private string contenttype;

Private string filename;

Private inputstream; // The name here cannot use "in"

Solution:

Inputstream = servletactioncontext. getservletcontext (). getresourceasstream (XXXX );

The type can be determined based on the suffix. For the corresponding relationship, see tomcat/CONF/Web. xml.

2. Set result in the configuration file:

  1. <Resulttype = "stream">
  2. <Paramname = "contenttype" >$ {contenttype} </param> <! -- Use the contenttype attribute in action to indicate the file type. You can also write application/octet-stream to indicate no limit. -->
  3. <Paramname = "inputname"> inputstream </param> <! --
    Read data from the stream whose attribute name is inputsteam in the action. You do not need to use an ognl expression to provide a byte stream. -->
  4. <Paramname = "contentdisposition"> attachment; filename = "$ {filename}" </param>
    <! --- Set the file name -->
  5. <Paramname = "buffersize"> 4096 </param>
    <! -- Set the buffer size -->
  6. </Result>

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.