Success Code:
Front interface jsp: <a style= "text-decoration:none;" href= "<%=path%>/main/frontnewsaction_downloadfile.action? Filename=<s:property value= "Filetitle"/> "> Downloads </a>Action File:
Private String fileName; Get Set method
Publicstring Getdownloadfilename () {string Downfilename=FileName; Try{downfilename=NewString (Downfilename.getbytes (), "Utf-8"); } Catch(Exception e) {e.printstacktrace (); } returnDownfilename; } //downloaded streams PublicInputStream getInputStream ()throwsfilenotfoundexception {String name= This. Getdownloadfilename (); File File=NewFile (Servletactioncontext.getservletcontext (). Getrealpath ("/file") + "/" +name); InputStream InputStream=Newfileinputstream (file); return Newfileinputstream (file); } //Download PublicString DownloadFile ()throwsException {return"DownloadFile"; }
<!--files Download- <result name= "DownloadFile" type= "stream" > <param name= "ContentType" > Application/octet-stream;charset=utf-8 </param> <param name= "Contentdisposition" > Attachment;filename= "${downloadfilename}" </param><param name= "InputName" >inputStream</param > <param name= "buffersize" >4096</param> </result>
The error occurs because the public InputStream getInputStream () method in the action returns a value of: Return Servletactioncontext.getservletcontext (). getResourceAsStream (Servletactioncontext.getservletcontext (). Getrealpath ("/file") + "/" +name);
caused by the exception
Learning content:
Configuration file:
<!--file download, support for Chinese attachment name---
<action name= "FileDownload"
class= "Com.test.action.filedown.FileDownloadAction" >
<result name= "Success" type= "Stream" >
<!--dynamic file download, do not know the future file type beforehand, then we can set its value to be: application/octet-stream;charset=iso8859-1, note must join CharSet, Otherwise, it may cause the downloaded file to fail at some point;
<param name= "ContentType" >
application/octet-stream;charset=iso8859-1
</param>
<param name= "Contentdisposition" >
<!--use the transcoded file name as the download file name, DownloadFileName property
Corresponding 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 portion of ${}, We can think of it as equivalent to +action. Getdownloadfilename (). -
attachment;filename="${downloadfilename}"
</param>
<param name= "InputName" >inputStream</param>
<param name= "BufferSize" >4096</param>
</result>
</action>
Action file:
----------------------------------------------------------
In action
----------------------------------------------------------
Private String filename;//The initial file name property specified by Param set get
/** file name Conversion code to prevent Chinese garbled */
Public String Getdownloadfilename () {
String filename=servletactioncontext.getrequest (). GetParameter ("FileName");
String downfilename = fileName;
try {
Downfilename = new String (Downfilename.getbytes (), "iso8859-1");
} catch (Exception e) {
E.printstacktrace ();
}
return downfilename;
}
Downloaded streams
Public InputStream getInputStream () {
String Name=this.getdownloadfilename ();
String Realpath=servletactioncontext.getservletcontext (). Getrealpath ("/uploadimages") + "/" +name; Path error
String 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. ");
}
return Servletactioncontext.getservletcontext (). getResourceAsStream (Realpath);//exception occurred
}
@Override
Public String Execute () throws Exception {
return SUCCESS;
}
Struts2 file download appears can not find a java.io.InputStream with the name error