(1) simple download Control
Filedown.html
- <A href = "Download. Action"> download a compressed file </a>
Struts. xml
- <Constant name = "struts. i18n. encoding" value = "GBK"/>
- <Package name = "Upload" extends = "struts-Default">
- <Action name = "Upload" class = "jcuckoo. uploadaction">
- <Interceptor-ref name = "fileupload">
- <Param name = "allowedtypes"> image/BMP, image/PNG, image/GIF, image/JPEG </param>
- <Param name = "maximumsize"> 20000 </param>
- </Interceptor-ref>
- <Interceptor-ref name = "defaultstack"/>
- <! -- Save the path savepath dependency injection -->
- <Param name = "savepath">/upload </param>
- <Result name = "input">/upload. jsp </result>
- <Result>/succ. jsp </result>
- </Action>
- <Action name = "Download" class = "jcuckoo. filedownloadaction">
- <Result name = "success" type = "stream">
- <Param name = "contenttype"> application/zip </param>
- <Param name = "inputname"> targetfile </param>
- <Param name = "contentdisposition"> attachment; filename = "$ {downloadfilename}" </param>
- <Param name = "buffersize"> 4096 </param>
- </Result>
- </Action>
- </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
- Public class filedownloadaction implements action {
- PrivateString inputpath =// Images // zhong.zip;// You can obtain it by searching for a database
- Public void setinputpath (string inputpath ){
- This. inputpath = inputpath;
- }
- /** Provide the converted file name for download */
- Public String getdownloadfilename () throws unsupportedencodingexception {
- String downfilename = NULL;
- Int I = inputpath. lastindexof ('//');
- Downfilename = inputpath. substring (I + 1 );
- Downfilename = new string (downfilename. getbytes (), "ISO8859-1 ");
- Return downfilename;
- }
- /*
- * The action used for download should return an inputstream instance. This method corresponds to the value of inputname in result as targetfile.
- */
- Public inputstream gettargetfile () throws exception {
- Return servletactioncontext. getservletcontext (). getresourceasstream (inputpath );
- }
- Public String execute () throws exception {
- Return success;
- }
- }
(2) download Authorization Control
You only need to make a simple modification to execute () above.
- Public StringExecute ()Throws exception
- {
- Actioncontext CTX = actioncontext. getcontext ();
- Map session = CTX. getsession ();
- String user = (string) Session. Get ("jcuckoo ");
- If (user! = NULL & User. Equals ("admin "))
- {
- Return success;
- }
- CTX. Put ("tip", "You have not logged on yet, or the user name you have logged on to is incorrect. Please log on again! ");
- Return login;
- }
Modify the corresponding struts. XML
- <Action name = "Download" class = "jcuckoo. filedownloadaction">
- <Result name = "success" type = "stream">
- <Param name = "contenttype"> application/zip </param>
- <Param name = "inputname"> targetfile </param>
- <Param name = "contentdisposition"> attachment; filename = "$ {downloadfilename}" </param>
- <Param name = "buffersize"> 4096 </param>
- </Result>
- <ResultName = "login">/input. jsp </result>
- </Action>