Struts2 File Download exception Solution

Source: Internet
Author: User

Can not find a java. Io. inputstream with the name [downloadfile] in the invocation stack.

If the preceding exceptions occur, there are usually two situations.

 

One is that the method for getting the file stream in the action does not match the one configured later,

Public inputstream getdownloadfile

(){},

(Is the first letter lower case after get consistent with the configuration parameters in struts)

<Param name = "inputname"> downloadfile
</Param>

 

Troubleshoot this situation

 

The second is caused by Chinese garbled characters.

 

In the request sent from the front-end, the download file name is Chinese and garbled characters are obtained in the background. All files cannot be found when the input stream is obtained,

Getdownloadfile () returns NULL, which will prompt you

Can not find a java. Io. inputstream with the name [downloadfile] in
Invocation stack.

The solution is to obtain and decode the file name in the request parameters.

Public void setfilename (string filename ){
String fname = servletactioncontext. getrequest (). getparameter ("FILENAME ");
Try {
/*
* UTF-8 decoding of the fname parameter, note: the actual UTF-8 decoding will use local encoding, the local is GBK.
* The request. setcharacterencoding decoding is invalid.
* Only the getdownloadfile () method can be decoded to find the requested file in the download directory.
**/
Fname = new string (fname. getbytes ("ISO-8859-1"), "UTF-8 ");
} Catch (exception e ){
E. printstacktrace ();
}
This. filename = fname;
}

 

Can be downloaded normally

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.