Struts2实现下载文件

Struts2框架和文件下载有关的类是org.apache.struts2.dispatcher.StreamResult,这个类的一些成员变量和下载有关,看源码

   //下载文件的类型
   protected String contentType = "text/plain";
   //下载文件的长度
    protected String contentLength;
    //
    protected String contentDisposition = "inline";
    //下载文件的文本编码
    protected String contentCharSet ;
    //指定文件输入流的名称,通常是InputStream的子类
    protected String inputName = "inputStream";
    protected InputStream inputStream; 
    //文件缓存大小
    protected int bufferSize = 1024;
    //是否允许缓存
    protected boolean allowCaching = true;

DownLoadAction.java

package com.struts2.download;

import java.io.IOException;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport {

    private int number;
    private String fileName;
    public void setNumber(int number) {
        this.number = number;
    }
    public int getNumber() {
        return number;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public InputStream getDownLoadFile() throws IOException {

        if(1==number){
            this.fileName="Dream.jpg";
            //获取资源路径
            return ServletActionContext.getServletContext().getResourceAsStream("upload/Dream.jpg");
        }
        else if(2==number){
            this.fileName="jd2chm源码生成chm格式文档.rar" ;  
            //解决乱码
            this.fileName=new String(this.fileName.getBytes("gbk"), "iso-8859-1");
            return ServletActionContext.getServletContext().getResourceAsStream("upload/jd2chm源码生成chm格式文档.rar");
        }
        return null;
    }

    @Override
    public String execute() throws Exception {

        return SUCCESS;
    }

}

filedownload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'filedownload.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>

<h2>下载内容</h2><br/>
 Dream.jsp <a href="${pageContext.request.contextPath}/DownLoadAction.action?number=1">点击下载</a><br/>
  jd2chm源码生成chm格式文档.rar<a href="${pageContext.request.contextPath}/DownLoadAction.action?number=2">点击下载</a><br/>

  </body>
</html>
</package>

    <package name="filedownload" extends="struts-default">
    <action name="DownLoadAction" class="com.struts2.download.DownLoadAction">
    <result name="success" type="stream">
    <param name="contentType">image/jpeg</param><!-- 指定下载文件类型 -->
    <param name="contentDisposition">attacthment;filename="${fileName}"</param><!-- filename="${fileName}"指定下载文件名 -->
    <param name="inputName">DownLoadFile</param>
    <param name="bufferSize">1024</param><!--指定下载文件缓存的大小  -->

    </result>
    </action>
    </package>

注意 DownLoadFile中DownLoadFile是DownLoadAction.java的getDownLoadFile方法去掉get的名称,这是约定的。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值