strut2文件下载及注意事项
2012-11-25 18:03
267 查看
不多说话,直接上代码:
文件保存在项目Test的根目录的resource文件夹下
Action代码:
struts.xml配置信息:
然后通过浏览器访问download.action就可以下载到text_store.txt
<--完-->
文件保存在项目Test的根目录的resource文件夹下
Action代码:
...... public class DownLoad extends ActionSupport{ public String downLoad(){ return SUCCESS; } public InputStream getInputStream throws Exception{ String path="/resource/test_store.txt"; //这个路径是相对于工程根目录的绝对路径,注意不要写错了,否则会抛出如下异常,本人亲自试过: //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. return ServletActionContext.getServletContext().getResourceAsStream(path); } ......
struts.xml配置信息:
...... <action name="download.*" class="com.system.action.DownLoad" method="downLoad"> <result name="success" type="stream"> <!--如果系统会自动到你的action中去找getInputStream方法;也可以为其他名称,但相应的方法名称也要改--> <param name="inputName">inputStream</param> <param name="bufferSize">4096</param> <!-- attachment的意思是直接下载,不让浏览器自动打开;filename指定下载文件的文件名,就是你在文件下载对话框中看到的文件名 --> <param name="contentDisposition">attachment:filename="text_save.txt"</param> </result> </action> ......
然后通过浏览器访问download.action就可以下载到text_store.txt
<--完-->
相关文章推荐
- jsp下载文件的实现方法及注意事项
- HttpServletResponse response(中文乱码、文件下载、定时刷新、控制缓存、重定向、注意事项)
- HttpServletResponse response(中文乱码、文件下载、定时刷新、控制缓存、重定向、注意事项)
- PHP连接SQLSERVER 注意事项(附dll文件下载)
- php生成csv文件并提供下载及相关注意事项
- 3.文件下载的一些注意事项
- 文件的上传和下载—上传的实现,注意事项
- 【JavaWeb-13】文件上传DiskFileItemFactory、ServletFileUpload以及注意事项、文件下载及注意事项
- thinkphp 文件下载实例 实现以及注意事项
- 文件的上传和下载—上传的实现,注意事项
- 动态生成 Excel 文件供浏览器下载的注意事项
- PHP 下载导出中文名的文件的编码注意事项
- QFTP的配置使用以及避免下载文件为0kb的注意事项
- struts2中文件下载的注意事项
- 编写文件上传和下载要注意的事项
- AFNetworking_网络文件下载上传详解(注意事项)
- PHP连接SQLSERVER 注意事项(附dll文件下载)
- servlet 文件下载及注意事项
- 动态生成 Excel 文件供浏览器下载的注意事项
- Java文件下载选择路径注意事项