Vidor_Chan
2015-04-12 23:11
<a href="download.action?filename=123图片.jpg">文件下载</a> <action name="download" class="com.pactera.action.FileDownloadAction"> <!-- <param name="inputPath">/</param> --> <result name="success" type="stream"> <param name="contentType">application/octet-stream</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="bufferSize">8192</param> </result> </action>
String filename;
public String getDownloadFileName() {
String downloadFileName = "";
try {
downloadFileName = URLEncoder.encode(filename,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return downloadFileName;
}
public InputStream getInputStream() throws IOException{
String path = ServletActionContext.getServletContext().getRealPath("/images");
String filePath = path + "\\" + filename;
File file = new File(filePath);
return FileUtils.openInputStream(file);
//return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
你要看他是在获取文件信息是报的错误,应该是在jsp中少了要下载文件的信息
<a href="download?filename=360截图20150601210855523.jpg">点击下载</a>
如果你下载文件时从页面获取文件的名称,如果文件名有中文的话,报
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.
后台控制台可以看到java.io.FileNotFoundException异常:
java.io.FileNotFoundException: C:\Users\Administrator\Pictures\360??????20150601210855523.jpg (文件名、目录名或卷标语法不正确。)
那么你在action中处理文件名称的时候需要对其进行转码,将中文文件名转码成能识别的文件名
String filePath = path + "\\" + new String(filename.getBytes("ISO-8859-1"),"UTF-8"));
这样解决了下载中文文件名报错的问题。前面的回答可用在文件固定的下载上,这个可以用在文件名不固定的下载上。
出现
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.
异常时,通常:
1,文件路径不对,根本就没获取到文件,可以将获得的InputStream打印,如果为null,则检查文件路径;
2,在action中没有写配置文件中"<param name="inputName">"后面属性的那个get方法.
3,以上均无问题,那就是使用
return ServletActionContext.getServletContext().getResourceAsStream("...")
获取流的时候,保证文件位置在 ServletContext 当中,就是说要在当前的应用上下文中,如果是从外部引入文件,比如:C:/Users/Administrator/Pictures/360截图20150601210855523.jpg ,要手动建一个流
public InputStream getInputStream(){ File file = new File(inputPath); InputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } return is; //ServletActionContext.getServletContext().getResourceAsStream(inputPath); }
以上应该能解决你的问题。
也是这个问题
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the
tag specified for this action.
File: org/apache/struts2/dispatcher/StreamResult.java
Line number: 237
Stacktraces
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check thetag specified for this action.
org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:277)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
Java中的文件上传下载
77391 学习 · 337 问题
相似问题
回答 2
回答 1