Struts2 Control File Download

Source: Internet
Author: User

(1) simple download Control

Filedown.html
  1. <A href = "Download. Action"> download a compressed file </a>

Struts. xml

  1. <Constant name = "struts. i18n. encoding" value = "GBK"/>
  2. <Package name = "Upload" extends = "struts-Default">
  3. <Action name = "Upload" class = "jcuckoo. uploadaction">
  4. <Interceptor-ref name = "fileupload">
  5. <Param name = "allowedtypes"> image/BMP, image/PNG, image/GIF, image/JPEG </param>
  6. <Param name = "maximumsize"> 20000 </param>
  7. </Interceptor-ref>
  8. <Interceptor-ref name = "defaultstack"/>
  9. <! -- Save the path savepath dependency injection -->
  10. <Param name = "savepath">/upload </param>
  11. <Result name = "input">/upload. jsp </result>
  12. <Result>/succ. jsp </result>
  13. </Action>
  14. <Action name = "Download" class = "jcuckoo. filedownloadaction">
  15. <Result name = "success" type = "stream">
  16. <Param name = "contenttype"> application/zip </param>
  17. <Param name = "inputname"> targetfile </param>
  18. <Param name = "contentdisposition"> attachment; filename = "$ {downloadfilename}" </param>
  19. <Param name = "buffersize"> 4096 </param>
  20. </Result>
  21. </Action>
  22. </Package>

This action is special because the result type is a stream (Stream), Because you do not need to specify the actual physical resources displayed, you do not need to specifyLocationAttribute, you only need to specifyInputnameProperty. This property points to the source of the downloaded object. It corresponds to an attribute in the action class and its type is inputstream. The following lists some download-related parameters:

Parameter description

Contenttype

Content type, which is consistent with the provisions in the MIME standard. For example, text/plain indicates plain text, text/XML indicates XML, image/GIF indicates GIF image, and image/JPEG indicates jpg image.

Inputname

The source code of the downloaded file. It corresponds to the attribute name of an inputstream type in the action class, for example, taking the attributeInputstreamThe getinputstream () method is required.

Contentdisposition

The file download process includes inline and attachment. The File Save dialog box is displayed. Otherwise, the browser tries to display the file directly.
Attachment; filename = "XXX. XXX"Indicates that the name saved during File Download should beXXX. XXX. If you write directlyFilename = "XXX. XXX"The default value isInlineThe browser will try to open it automatically, which is equivalent to the following statement:Inline; filename = "XXX. XXX"

Buffersize

The size of the download buffer.
Here, the contenttype attribute and contentdisposition correspond to the headers in the HTTP response respectively.Content-TypeAndContent-DispositionHeader.

Filedownloadaction. Java

  1. Public class filedownloadaction implements action {
  2. PrivateString inputpath =// Images // zhong.zip;// You can obtain it by searching for a database
  3. Public void setinputpath (string inputpath ){
  4. This. inputpath = inputpath;
  5. }
  6. /** Provide the converted file name for download */
  7. Public String getdownloadfilename () throws unsupportedencodingexception {
  8. String downfilename = NULL;
  9. Int I = inputpath. lastindexof ('//');
  10. Downfilename = inputpath. substring (I + 1 );
  11. Downfilename = new string (downfilename. getbytes (), "ISO8859-1 ");
  12. Return downfilename;
  13. }
  14. /*
  15. * The action used for download should return an inputstream instance. This method corresponds to the value of inputname in result as targetfile.
  16. */
  17. Public inputstream gettargetfile () throws exception {
  18. Return servletactioncontext. getservletcontext (). getresourceasstream (inputpath );
  19. }
  20. Public String execute () throws exception {
  21. Return success;
  22. }
  23. }

(2) download Authorization Control
You only need to make a simple modification to execute () above.

  1. Public StringExecute ()Throws exception
  2. {
  3. Actioncontext CTX = actioncontext. getcontext ();
  4. Map session = CTX. getsession ();
  5. String user = (string) Session. Get ("jcuckoo ");
  6. If (user! = NULL & User. Equals ("admin "))
  7. {
  8. Return success;
  9. }
  10. CTX. Put ("tip", "You have not logged on yet, or the user name you have logged on to is incorrect. Please log on again! ");
  11. Return login;
  12. }

Modify the corresponding struts. XML

  1. <Action name = "Download" class = "jcuckoo. filedownloadaction">
  2. <Result name = "success" type = "stream">
  3. <Param name = "contenttype"> application/zip </param>
  4. <Param name = "inputname"> targetfile </param>
  5. <Param name = "contentdisposition"> attachment; filename = "$ {downloadfilename}" </param>
  6. <Param name = "buffersize"> 4096 </param>
  7. </Result>
  8.  <ResultName = "login">/input. jsp </result>
  9. </Action>

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.