Struts2 file download appears can not find a java.io.InputStream with the name error

Source: Internet
Author: User

Today, the following error occurred while using struts2 to download the file:

Servlet.service () forServletdefaultthrew exception Java.lang.IllegalArgumentException:Can not find a java.io.InputStream with the name [Imagestre AM] in the invocation stack.
Check the<param name= "InputName" > tag specified for Thisaction. At Org.apache.struts2.dispatcher.StreamResult.doExecute (Streamresult.java:189) at Org.apache.struts2.dispatcher.StrutsResultSupport.execute (Strutsresultsupport.java:178) at Com.opensymphony.xwork2.DefaultActionInvocation.executeResult (Defaultactioninvocation.java:348) at Com.opensymphony.xwork2.DefaultActionInvocation.invoke (Defaultactioninvocation.java:253) at Com.best.top.validate.TopInterceptor.intercept (Topinterceptor.java:47) at com.opensymphony.xwork2.defaultactioninvocation$2.doProfiling (defaultactioninvocation.java:224) at com.opensymphony.xwork2.defaultactioninvocation$2.doProfiling (defaultactioninvocation.java:223) at Com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile (Utiltimerstack.java:455) at Com.opensymphony.xwork2.DefaultActionInvocation.invoke (Defaultactioninvocation.java:221) at Org.apache.struts2.impl.StrutsActionProxy.execute (Strutsactionproxy.java:50) at Org.apache.struts2.dispatcher.Dispatcher.serviceAction (Dispatcher.java:504) at Org.apache.struts2.dispatcher.FilterDispatcher.doFilter (Filterdispatcher.java:419)

To tell the truth this hint is really misleading suspicion, at first thought is the wrong name, estimated that the general people see this hint like this. Then look at the source code of Streamresult only to find out because InputStream is null for the sake of Khan one. Look at the Source:

if(InputStream = =NULL) {                       //Find the InputStream from the invocation variable stackInputStream =(InputStream) Invocation.getstack (). Findvalue (Conditionalparse (InputName, invocation)); }                         if(InputStream = =NULL) {String msg= ("Can not find a java.io.InputStream with the name [" + InputName + "] in the invocation stack." + "Check the <param name=\" inputname\ "> tag specified for this action.");                       Log.error (msg); Throw Newillegalargumentexception (msg); }  

If you encounter such problems, print directly

InputStream in=Servletactioncontext.getservletcontext (). getResourceAsStream (Realpath); System.out.println (in);

If printing is null, congratulations, the problem is solved, the problem is the flow of the Realpath path error, still do not understand the look down, strange, my configuration should be correct

On the page:

<href= "Filedownload.action?filename=<s:property value ="/> "> Download this picture </a>

In Struts.xml:

<!--file download, support Chinese attachment name -   <Actionname= "FileDownload"class= "Com.test.action.filedown.FileDownloadAction">    <resultname= "Success"type= "Stream">     <!--dynamic file download, in advance do not know the future file type, then we can set its value to: application/octet-stream;charset=iso8859-1, be sure to join CharSet, Otherwise, it may cause the downloaded file to go wrong; -     <paramname= "ContentType">application/octet-stream;charset=iso8859-1</param>     <paramname= "Contentdisposition">attachment;filename= "${downloadfilename}"</param>     <!--using the transcoded file name as the download file name, the DownloadFileName property corresponds to the method in the Action Class Getdownloadfilename () where the special code is ${downloadfilename}, Its effect is equivalent to running the value of the property of the action object dynamically populated in the middle of ${}, we can consider it equivalent to +action. Getdownloadfilename ().  -     <paramname= "InputName">InputStream</param>     <paramname= "BufferSize">4096</param>    </result>   </Action>

In action:

PrivateString FileName;//The initial file name property specified by param set get/**file name Conversion code to prevent Chinese garbled characters*/ Publicstring Getdownloadfilename () {string FileName=servletactioncontext.getrequest (). GetParameter ("FileName"); String Downfilename=FileName; Try{downfilename=NewString (Downfilename.getbytes (), "Iso8859-1"); } Catch(Exception e) {e.printstacktrace (); }   returnDownfilename;}//downloaded streams PublicInputStream getInputStream () {String name= This. Getdownloadfilename ();//String Realpath=servletactioncontext.getservletcontext (). Getrealpath ("/uploadimages") + "/" +name; Path errorString realpath= "/uploadimages/" +name; InputStream in=Servletactioncontext.getservletcontext (). getResourceAsStream (Realpath); if(NULL==In ) {System.out.println ("Can not find a java.io.InputStream with the name [InputStream] in the invocation stack. Check the <param name=\ "inputname\" > tag specified for this action. Verify that the file download path in the action is correct. "); }   returnServletactioncontext.getservletcontext (). getResourceAsStream (Realpath);} @Override PublicString Execute ()throwsException {returnSUCCESS;}

Struts2 file download appears can not find a java.io.InputStream with the name error

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.