Struts2文件下载

上一篇刚刚写了一个上传的,今天再来复习一下Struts2的文件下载

Struts2单多文件上传:http://blog.csdn.net/u012219290/article/details/45671657

简介:
struts的下载action与配置一个普通的action基本一致,主要的地方在于result的type配置为stream
stream类型的result配置主要需要四个属性:

contentType:
指定下载文件的文件类型,与互联网MIME标准中的规定类型一致HTTP Content-type
inputName:
指定下载文件入口输入流
contentDisposition:
文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对
框,否则浏览器会尝试直接显示文件。取值为:attachment;filename=”struts.txt” ,表示文件下载的时候保存的名字应为
struts.txt 。如果直接写filename=”struts.txt” >那么默认情况是代表inline浏览器会尝试自动打开它,等价于这样的写法:
inline; filename=”struts.txt”
bufferSize:
指定下载文件时的缓冲大小

注意:inputName参数的配置必须对应action中返回流的get方法一致。
比如:

//配置为
<param name="inputStream">inputStream</param>
//对应action中的方法
public InputStream getInputStream(){}

先看效果:

一.访问http://localhost:8080/Struts2Download/getFileNames.action获取可下载的文件列表

这里写图片描述

二.点击下载将对应文件保存到指定目录(图略)

工程的创建步骤和工程的目录与Struts2文件上传相同
这里直接上代码

1.struts.xml

<?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>

    <!--将默认上传的文件大小改为500MB -->
    <constant name="struts.multipart.maxSize" value="524288000" />
    <!--编码-->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <!--开发模式-->
    <constant name="struts.devMode" value="true" />


    <package name="default" extends="json-default" namespace="/">

        <!-- 文件下载  -->
        <action name="fileDownload" class="action.DownloadAction">
            <param name="filePath">upload</param>
            <result name="success" type="stream">
                <param name="contentType">application/octet-stream;charset=UTF-8</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>

        <!--获取可下载的文件-->
        <action name="getFileNames" class="action.DownloadAction" method="getFileNames">
            <!--下载文件的目录-->
            <param name="filePath">upload</param>
            <result name="success">download.jsp</result>
        </action>


    </package>

</struts>

2.DownloadAction.java

package action;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Struts2文件下载
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class DownloadAction extends ActionSupport {

    private String fileName;// 文件名
    private String filePath;// 文件存放的路径
    private List<String> nameList = new ArrayList<String>();// 可下载的文件集



    /**
     * 获取所有可供下载的文件
     * @return
     */
    public String getFileNames() {

        String realPath = ServletActionContext.getServletContext().getRealPath(
                File.separator + filePath);
        File file = new File(realPath);
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()) {
                nameList.add(f.getName());
            }
        }

        return SUCCESS;

    }

    /**
     * 文件传输流
     * @return
     * @throws Exception
     */
    public InputStream getInputStream() throws Exception {

        InputStream input = ServletActionContext.getServletContext()
                .getResourceAsStream(
                        File.separator + filePath + File.separator + fileName);

        //编码转换,防止页面显示文件名乱码。这里文件放在tomcat中,tomcat已经设置编码为utf-8
        this.fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");

        return input;
    }


    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public List<String> getNameList() {
        return nameList;
    }

    public void setNameList(List<String> nameList) {
        this.nameList = nameList;
    }

}

3.download.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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>文件下载</title>
</head>
<body>
    <table style="width: 50%;text-align:center" border="1">
        <caption style="color:red;font-size:20px">文件列表</caption>
        <tr style="background-color:#99CCCC">
            <td>序号</td><td>文件名</td><td>操作</td>
        </tr>

        <c:forEach items="${nameList}" var="fileName" varStatus="status">
            <tr>
                <td>${status.count}</td>
                <td>${fileName}</td>
                <td>
                    <a href="fileDownload.action?fileName=${fileName}">下载</a>
                </td>
            </tr>
        </c:forEach>

    </table>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值