struts2文件下载 出现Can not find a java.io.InputStream with the name的错误
成功代码:
前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontNewsAction_downloadFile.action?fileName=<s:property value="fileTitle"/>">下载</a> Action文件:
private String fileName;//get set方法
public String getDownloadFileName() {
String downFileName = fileName;
try {
downFileName = new String(downFileName.getBytes(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return downFileName;
} //下载的流
public InputStream getInputStream() throws FileNotFoundException {
String name=this.getDownloadFileName();
File file = new File(ServletActionContext.getServletContext().getRealPath("/file")+"/"+name);
InputStream inputStream = new FileInputStream(file);
return new FileInputStream(file);
} // 下载
public String downloadFile() throws Exception {
return "downloadFile";
}
<!-- 文件下载 -->
<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>
出现错误时是由于在action中的public InputStream getInputStream()方法返回值为:return ServletActionContext.getServletContext().getResourceAsStream(ServletActionContext.getServletContext().getRealPath("/file")+"/"+name);
造成的异常
学习内容:
配置文件:
<!-- 文件下载,支持中文附件名 -->
<action name="fileDownload"
class="com.test.action.filedown.FileDownloadAction">
<result name="success" type="stream">
<!--
动态文件下载的,事先并不知道未来的文件类型,那么我们可以把它的值设置成为:application/octet-
stream;charset=ISO8859-1 ,注意一定要加入charset,否则某些时候会导致下载的文件出错; -->
<param name="contentType">
application/octet-stream;charset=ISO8859-1
</param>
<param name="contentDisposition">
<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
对应action类中的方法 getDownloadFileName()
其中特殊的代码就是${downloadFileName},它的效果相当于运行的时候将action对象的属性的取值动态的填充在${}中间的部分,我
们可以认为它等价于+action. getDownloadFileName()。 -->
attachment;filename="${downloadFileName}"
</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">4096</param>
</result>
</action>
action文件:
----------------------------------------------------------
action中
----------------------------------------------------------
private String fileName;// 初始的通过param指定的文件名属性 set get
/** 文件名 转换编码 防止中文乱码*/
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;
}
//下载的流
public InputStream getInputStream() {
String name=this.getDownloadFileName();
// String realPath=ServletActionContext.getServletContext().getRealPath("/uploadImages")+ "/"+name; 路径错误
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.检查action中文件下载路径是否正确.");
}
return ServletActionContext.getServletContext().getResourceAsStream(realPath);//出现异常
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
struts2文件下载 出现Can not find a java.io.InputStream with the name的错误的更多相关文章
- struts2文件下载出现Can not find a java.io.InputStream with the name的错误
今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArg ...
- Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.
1.错误描写叙述 八月 14, 2015 4:22:45 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error 严重: Excepti ...
- java.io.StreamCorruptedException: invalid type code: AC错误的解决方法
问题描述: 在向一个文件写入可序列化对象时,每次只想向文件的末尾添加一个可序列化的对象,于是使用了FileOutputStream(文件名,true)间接的构建了ObjectOutputStream流 ...
- 【java】io流之字节输入流:java.io.InputStream类及子类java.io.FileInputStream
package 文件操作; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impor ...
- Error: Default interface methods are only supported starting with Android N (--min-api 24): java.io.InputStream org.apache.poi.sl.usermodel.ObjectShape.readObjectData()
项目运行的时候,如果报错 Error: Default interface methods are only supported starting with Android N (--min-api ...
- 关于Java IO InputStream 的一点整理!
程序的开发其中一直在用文件的读写.可是对于java其中输入流以及输出流仅仅是会用不理解,一直以来想搞清楚其,可是一直没有运行(悲剧).今天早上抽出半个小时通过JDK API1.6.0中文版帮助逐步的了 ...
- java io InputStream 转 byte
InputStream is ; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024] ...
- Struts2文件下载
1). Struts2 中使用 type="stream" 的 result 进行下载 2). 可以为 stream 的 result 设定如下参数 contentType: 结果 ...
- Struts2 文件下载
使用Struts2做一个简单的文件下载. 首先,导包,写配置文件就不说了. 进入主题. 文件下载操作类:FileDownload.java import java.io.InputStream; im ...
随机推荐
- tomcat 默认项目设置
正常情况下,我们启动tomcat后,直接输入“http://localhost:端口/“ 后,默认访问道是webapp目录下的ROOT应用. 我们要通过上述方式访问自己的应用,有俩种方式. 第一:把自 ...
- 我是红领巾,分享2014 google不能用的方法。
那啥已经20天打不开了. 得爬qiang. 今天无意间发现一个好东东. 特记录一下. 360浏览器设置 1. 工具菜单==>选项==>高级设置==>管理搜索引擎 . 2. ...
- HelloX操作系统与中国移动OneNET物联网平台成功完成对接
HelloX成功与中国移动物联网平台对接 经过HelloX项目组同仁的努力,尤其是Tywin(@飓风)的努力下,HelloX最新版本V1.78已成功与中国移动OneNET(open.iot.10086 ...
- NALU(NAL单元)
一 NALU类型 标识NAL单元中的RBSP数据类型,其中,nal_unit_type为1, 2, 3, 4, 5及12的NAL单元称为VCL的NAL单元,其他类型的NAL单元为非VCL的NAL ...
- LA 3708 Graveyard 墓地雕塑 NEERC 2006
在一个周长为 10000 的圆上等距分布着 n 个雕塑.现在又有 m 个新雕塑加入(位置可以随意摆放),希望所有 n + m 个雕塑能在圆周上均匀分布.这就需要移动一些原有的雕塑.要求 n 个雕塑移动 ...
- 鼠标悬停css3动画效果
下载Demo 效果预览 html: <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- Map/Reduce之间的Partitioner接口
一.Partitioner介绍 Partitioner的作用是对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reduce处理,它直接影响Reduce阶段的负载均衡(个人理解:就是 ...
- 如何设置适当的ramp-up period值
如何设置适当的值并不轻易. 首先,假如要使用大量线程的话,ramp-up period 一般不要设置成零. 因为假如设置成零,Jmeter将会在测试的开始就建立全部线程并立即发送访问请求, 这样一来就 ...
- C++类与对象
[1]类的内存问题 类是抽象的,不占用内存,而对象是具体的,占用 存储空间.在一开始时弄清对象和类的关系是十分 重要的.[2]类的声明 如果在类的定义中既不指定private也不指定public,则系 ...
- C字符串和C++中string的区别 &&&&C++中int型与string型互相转换
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...