Struts uploads and downloads Resources

Source: Internet
Author: User

The following code downloads all types of resources!

Public actionforward downloaddata (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception {
Actionforward forward = NULL;
String newsid = request. getparameter (constants. news_id). Trim ();
Request. setcharacterencoding ("gb2312 ");
Response. setcontenttype ("text/html; charset = gb2312 ");
Actionerrors errors = new actionerrors ();
Try {
Newscnt CNT = This. showservice. searchnews (newsid );
String filepath = CNT. getcnturl ();
// Filepath = urlencoder. encode (filepath, "UTF-8 ");
Log. debug ("----- >>>> filepaht =" + filepath );
//// Set to download
String filename = filepath. substring (
Filepath. lastindexof ("//") + 1, filepath. lastindexof ("."));
String Postfix = filepath. substring (filepath. lastindexof (".") + 1,
Filepath. Length ());
Log. debug ("----- >>>> filename =" + filename );
Response. setcontenttype ("application/X-download ");
Response. addheader ("content-disposition", "attachment; filename ="
+ "Download." + postfix );
Outputstream out = response. getoutputstream ();
Fileinputstream in = new fileinputstream (filepath );
Byte [] buffer = new byte [1024];
Int Len = 0;
While (LEN = in. Read (buffer)> 0 ){
Out. Write (buffer, 0, Len );
}
Out. Flush ();
Log. debug ("data download successful! ");
Forward = mapping. findforward ("downloaddatapath ");
} Catch (exception e ){
Log. debug ("exception occurred during data download:" + E. getmessage ());
Errors. Add (constants. error_key, new actionerror (
"Download. Data. Exception", messageresources
. Getmessageresources (constants. resource_key )));
Forward = mapping. getinputforward ();
}
Return forward;
}

Here are some notes to be aware!

1. Define the output type and set the output file header

Response. setcontenttype ("application/X-download ");
Response. addheader ("content-disposition", "attachment; filename ="
+ "Download." + postfix );

(1) When downloading an Excel file, set it as follows:

// Set the output file header
Response. setheader ("content-disposition ",
"Attachment?filename=module.xls ");
// Define the output type
Response. setcontenttype ("application/MSExcel ");

(2) set the following when downloading a Word file:

// Set the output file header
Response. setheader ("content-disposition ",
"Attachment?filename=module.doc ");
// Define the output type
Response. setcontenttype ("application/MSWord ");

2. The file name cannot be Chinese (this is not yet clear)
Response. addheader ("content-disposition", "attachment; filename ="
+ "Download." + postfix );

That is to say, "Download" cannot be Chinese. You have tried to use the source file name as the download file name. However, when the file name contains Chinese characters, it cannot be downloaded.

Therefore, the file name is changed to "Download ". The above Postfix is the extension

3. Up to now, the settings of the file header and the definition of the output type have not been fully clarified.

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.