27.struts2_文件的下载
来源:互联网 发布:js转换时间格式的方法 编辑:程序博客网 时间:2024/06/11 05:42
Struts2专门为文件下载提供了一种Stream结果类型,在使用Stream结果时,不必准备一个JSP页面。
可为result 设定如下参数:
contentType: 结果类型
contentLength:下载长度
contentDisposition:设定相应头,是一个文件下载类型。一般取值格式为: attachment;filename=“document.pdf”.
inputName: 指定文件输入流的 getter定义的属性的名字,默认为 inputStream
bufferSize:缓存的大小 默认为1024
allowCaching:是否允许使用缓存
contentCharSet:指定下载的字符集
以上参数也可以在Aciton中以getter方法的方式提供。
public class testDownloadAction extends ActionSupport{/** * */private static final long serialVersionUID = 1L;private String contentType;private long contentLength;private String contentDisposition;private InputStream inputStream;//提供一个输入流public InputStream getInputStream() {return inputStream;}public String getContentType() {return 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 void setContentType(String contentType) {this.contentType = contentType;}@Overridepublic String execute() throws Exception {//确定各个成员变量的值contentType="text/html";contentDisposition=new String("attachment;filename='綿谷の.html'".getBytes(), "ISO8859-1"); //有中文需要转码,不然下载不识别System.out.println("------"+contentDisposition);ServletContext sac=ServletActionContext.getServletContext();String fileName=sac.getRealPath("/files/知乎.html");inputStream=new FileInputStream(fileName);contentLength=inputStream.available();return super.execute();}}
struts.xml部分内容
<action name="testDownload" class="com.hcx.app.testDownloadAction"><result name="success" type="stream" ><param name="bufferSize" >2048</param></result></action>
阅读全文
0 0
- 27.struts2_文件的下载
- Struts2_文件下载
- struts2_文件下载
- Struts2_文件下载
- 42. Struts2_文件的上传
- 26.struts2_文件的上传
- Struts2_文件上传_Demo
- struts2_文件上传下载
- Struts2_单文件上传
- Struts2_多文件上传
- struts2_文件上传
- Struts2_文件上传
- Struts2_事物管理&文件上传
- Struts2_事物管理&文件上传
- struts2_梦开始的时间
- Struts2_输入校验的流程
- struts2_拦截器的使用
- struts2_拦截器的原理
- Android按键监听和AsyncTask异步处理
- js select 循环获取option的值
- Ubuntu+LeapMotion 环境配置
- 从零到独自开发一个网站(后端)
- redis (linux下安装)
- 27.struts2_文件的下载
- 寻找一个数组中的主元素
- bootstrap之初次学习简单小记
- Android UI学习|对View的onMeasure方法理解
- 产品设计 用户体验优化之兼容PC与移动端的回车搜索响应事件设计
- 简单的表单传递
- 支付宝、微信付款二维码 自定义中间图片
- 【TensorLayer】建立在TensorFlow上的深度学习(Deep Learning)与增强学习(Reinforcement Learning) 软件库
- Linux 安装配置ftp,并分配用户