1). Struts2 中使用 type="stream" 的 result 进行下载

2). 可以为 stream 的 result 设定如下参数

contentType: 结果类型
contentLength: 下载的文件的长度
contentDisposition: 设定 Content-Dispositoin 响应头. 该响应头指定接应是一个文件下载类型, 一般取值为  attachment;filename=document.pdf.

inputName: 指定文件输入流的 getter 定义的那个属性的名字. 默认为 inputStream

bufferSize: 缓存的大小. 默认为 1024
allowCaching: 是否允许使用缓存
contentCharSet: 指定下载的字符集

3). 以上参数可以在 Action 中以 getter 方法的方式提供!

4). 示例:

jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="downLoad">点击下载success.jsp文件</a>
</body>
</html>

Action:

package com.elgin.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction 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 { //确定各个成员属性的值:
this.contentType="text/html";
this.contentDisposition="attachment;filename=success.jsp";
String path=ServletActionContext.getServletContext().getRealPath("/files/success.jsp");
File file=new File(path);
this.inputStream=new FileInputStream(file);
this.contentLength=inputStream.available();
return SUCCESS; } }

struts2配置文件配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="com" extends="struts-default">
<action name="downLoad" class="com.elgin.action.DownloadAction">
<result type="stream">
<param name="bufferSize">2048</param>
</result>
</action> </package>
</struts>

Struts2文件下载的更多相关文章

  1. struts2文件下载 <result type="stream">

    <!--struts.xml配置--> <action name="download" class="com.unmi.action.DownloadA ...

  2. struts2 中 result type="stream"

    Stream result type是Struts2中比较有用的一个feature.特别是在动态生成图片和文档下载的情况下 1:图片验证码: Action类,action主要要提供一个获取InputS ...

  3. Struts2 中result type属性说明

    Struts2 中result type属性说明 首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-t ...

  4. struts2 action result type类型

    struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...

  5. Struts2 语法--result type

    result type: dispatcher,redirect:只能跳转到jsp,html之类的页面,dispatcher属于服务器跳转, redirect属于客户端跳转 chain: 等同于for ...

  6. Struts2 中 result type=”json” 的参数解释

    转自:http://wangquanhpu.iteye.com/blog/1461750 1, ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction 继承 ...

  7. jquery序列化from表单使用ajax提交返回json数据(使用struts2注解result type = json)

    1.action类引入struts2的"json-default"拦截器栈 @ParentPackage("json-default") //示例 @Paren ...

  8. struts2里result类型Stream的参数配置

    转自:https://blog.csdn.net/q714699280/article/details/51756126 contentType 内容类型,和互联网MIME标准中的规定类型一致,例如t ...

  9. Struts2文件下载浅析

    Struts2极大的简化了文件上传和下载,本文将主要介绍一下Struts2文件下载的实现1.功能主要是,在下载页面点击下载后,则下载相应的文件 2.代码部分jsp页面downloadPage:< ...

随机推荐

  1. jstl的forEach使用和jstl变量实现自增

    <c:forEach items="${reallyChooseSubjectList}" var="reallyChooseSubject"> & ...

  2. 多线程操作(循环往listbox中添加数据)

    一.先造一个窗体,其中就开始按钮,暂停按钮,以及listbox文本框 二.当点击开始的时候,数据会无限的往listbox中加,为了防止卡住和提升效率,便造了一个新的线程来执行开始操作 namespac ...

  3. HDU 4998 (点的旋转) Rotate

    为了寻找等效旋转操作,我们任选两个点P0和Q0,分别绕这n个点旋转一定的角度后最终得到Pn和Qn 然后已知:P0和Pn共圆,Q0和Qn共圆.所以要找的等效旋转点就是这两个线段的垂直平分线交点O. 等效 ...

  4. HTML元素margin、padding的默认值

    HTML元素margin.padding的默认值 element margin(单位像素) padding html 0 0 body 8 0 div 0 0 h1 21 0 h2 19 0 19 0 ...

  5. UVa 10498 Happiness! (线性规划)

    题意 将N种食品分给m个参赛选手,一个单位的某食品给某个选手一定满足度,每个选手有一个最大满足度.为了避免浪费,分给每一个选手的食品都不超越选手的满足度.已知的各种食品的单价,求最多可以花的钱. 思路 ...

  6. 关闭oom killer

    最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通常是因为某时刻应用程序大量请求内存导致系统 ...

  7. 20160127.CCPP体系详解(0006天)

    程序片段(01):msg.c 内容概要:线程概念 #include <stdio.h> #include <stdlib.h> #include <Windows.h&g ...

  8. id to load is required for loading----id被要求加载exception

    表示id主键 没有找到,可能是数据库中的主键设置了not null 而你没有给予主键 还有就是没有传递主键到 数据库中

  9. Heritrix源码分析(十一) Heritrix中的URL--CandidateURI和CrawlURI以及如何增加自己的属性(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/649889 本博客已迁移到本人独立博客: http://www.yun5u.com/ ...

  10. 【WebApi】————.net WebApi开发(一)

    2013年08月08日 ⁄ 综合 ⁄ 共 554字 ⁄ 字号 小 中 大 ⁄ 评论关闭 [1].部署环境.net4及以上版本. [2].vs2010  开发需单独安装vs2010 sp1和mvc4 m ...