struts2文件下载
2015-11-06 17:42
369 查看
struts2文件下载不需要链接到另一个jsp页面,故只需要一个超链接和一个处理文件下载的action类即可。
下面来看struts2文件下载的具体步骤和细节。
这是一个jsp页面的超链接,负责链接到服务器的文件流。
在看action类的内容前有必要了解一下struts2文件下载的几个相关的文件属性
contentType 【结果类型】 默认值是 default =
contentLength 下载的文件的长度
contentDisposition 设定Content-Disposition响应头,该响应头指定接应是一个文件下载类型,一般取值为
attachment;filename="document.pdf".
inputName 指定文件输入流的getter定义的那个属性的名字,默认为
bufferSize 缓存大小,默认为
allowCaching 是否允许使用缓存 默认为true
contentCharSet 指定下载的字符集
这几个属性都可以在struts.xml中定义或者在action里面以get的方法提供。
下面来看action类的内容。
上面的action分别声明了这几个属性
这表示动态生成
下面来看struts.xml的配置
标红的地方必须注意:该处的type值必须为stream,不然没办法struts2框架的文件下载功能
下面来看struts2文件下载的具体步骤和细节。
这是一个jsp页面的超链接,负责链接到服务器的文件流。
<a href="testDownload">Down Load</a>
在看action类的内容前有必要了解一下struts2文件下载的几个相关的文件属性
contentType 【结果类型】 默认值是 default =
text/plain
contentLength 下载的文件的长度
contentDisposition 设定Content-Disposition响应头,该响应头指定接应是一个文件下载类型,一般取值为
attachment;filename="document.pdf".
inputName 指定文件输入流的getter定义的那个属性的名字,默认为
inputStream
bufferSize 缓存大小,默认为
1024
allowCaching 是否允许使用缓存 默认为true
contentCharSet 指定下载的字符集
这几个属性都可以在struts.xml中定义或者在action里面以get的方法提供。
下面来看action类的内容。
package com.mxf.action; import java.io.FileInputStream; import java.io.InputStream; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class Download extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String contentType; private long contentLength; private String contentDisposition ; private InputStream inputStream; public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public long getContentLength() { return contentLength; } public void setContentLength(long contentLength) { this.contentLength = contentLength; } public String getContentDisposition() { return contentDisposition; } public void setContentDisposition(String contentDisposition) { this.contentDisposition = contentDisposition; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } @Override public String execute() throws Exception { contentType = "text/html";//指定接收的文件类型为html contentDisposition = "attachment;filename=iterator.html"; ServletContext servletContext = ServletActionContext.getServletContext(); String fileName = servletContext.getRealPath("/files/iterator.html");//项目webcontent下files文件夹下的<span style="font-family: Arial, Helvetica, sans-serif;">iterator.html</span> inputStream = new FileInputStream(fileName); contentLength = inputStream.available(); return SUCCESS; } }
上面的action分别声明了这几个属性
<span style="white-space:pre"> </span>private String contentType; private long contentLength; private String contentDisposition ; private InputStream inputStream;
这表示动态生成
下面来看struts.xml的配置
<span style="white-space:pre"> </span><action name="testDownload" class="com.mxf.action.Download"> <result<span style="background-color: rgb(255, 0, 0);"> type="stream"</span>> <param name="bufferSize">2048</param> </result> </action>
标红的地方必须注意:该处的type值必须为stream,不然没办法struts2框架的文件下载功能
相关文章推荐
- Java中的向上转型和向下转型
- 从本地安装Eclipse的SVN插件详解
- Error:Internal error: (java.io.FileNotFoundException) D:\code\project\.idea\misc.xml (The sys
- spring mvc中处理多对多的同时存储
- Josephus环类问题,java实现
- 服务端工程师入门与进阶 Java 版
- JavaScirpt学习经历
- Java读取properties配置文件时,中文乱码解决方法
- spring ioc原理(看完后大家可以自己写一个spring)
- kettle crontab java: command not found
- myeclipse三个地方的java版本统一
- Spring 整合 Mina
- Java的IO流的操作和一些理解
- Javapns-sdk16-2.2.1多线程推送
- java 参数传递 空对象 null
- Spring使用session,request,global sessiong作用域出现异常解决方法
- Java:类与继承
- 实战企业项目--springMVC+Mybatis
- Java编程中的异常和错误你遇到过哪些?
- java中HashSet详解