zoukankan      html  css  js  c++  java
  • 通过Struts2下载文件, 流式.

    处理下载的Action:

    package com.jyu.maven.MyWebApp;

    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionSupport;

    public class DownloadFileAction extends ActionSupport {

    private static String ROOTDIR = "d:\\"; //文件存放目录

    private String downFileName;
    private String mimeType;
    private InputStream fileInputStream;

    public InputStream getFileInputStream() {// 下载文件的输出流
    File file = new File(ROOTDIR, downFileName);

    try {
    fileInputStream = new FileInputStream(file);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.err.println("路径有问题");
    }
    if (fileInputStream == null) {
    fileInputStream = new ByteArrayInputStream(
    "Sorry,File not found !".getBytes());
    }
    return fileInputStream;
    }

    public String getMimeType() {
    return mimeType;
    }

    public String getDownFileName() {
    String FileName = downFileName;

    try {
           //对文件名转码, 不然在下载的时候文件名是乱码.
    FileName = new String(FileName.getBytes(), "ISO8859-1");

    } catch (UnsupportedEncodingException e) {

    e.printStackTrace();

    }
    return FileName;
    }

    public void setDownFileName(String downFileName) {
    this.downFileName = downFileName;
    }

    public String execute() throws Exception {
    File file = new File(ROOTDIR, downFileName);
         //在响应头里加入文件长度信息
    HttpServletResponse response = ServletActionContext.getResponse();
    response.addHeader("content-length", String.valueOf(file.length()));
     
         //获取文件类型
    mimeType = ServletActionContext.getServletContext().getMimeType(
    downFileName);
    return SUCCESS;
    }

    }

    struts.xml的配置:

    <action name="fileDownload" class="com.jyu.maven.MyWebApp.DownloadFileAction">
    <!-- 设置文件名参数,由页面上传入 -->
    <param name="downFileName"></param>
    <result name="success" type="stream">
    <!-- 下载文件类型定义 -->
    <param name="contentType">${mimeType}</param>
    <!-- 下载文件处理方法 -->
    <param name="contentDisposition">
    attachment;filename="${downFileName}"
    </param>
    <!-- 下载文件输出流定义 getFileInputStream() -->
    <param name="inputName">fileInputStream</param>
    <param name="bufferSize">1024</param>
    </result>
    </action>


    在html页面, 将下载链接指定到fileDownload.action, 并将downFileName作为参数传入, 如果存在该文件, 就可以下载文件了. 

    这种方式的确定是下载文件是流式的, 迅雷等下载工具无法暂停下载, 暂停后需要重新开始.



  • 相关阅读:
    Sublime Text 2快捷键大全
    JavaSE
    Ubuntu16.04 install ideaIC-2017.2.5.tar.gz
    Ubuntu/CentOS hadoop-2.x Cluster Setup
    Ubuntu16.04 install hadoop-2.8.1.tar.gz Cluster Setup
    12file
    11input/output
    10function
    09FlowControl
    08test
  • 原文地址:https://www.cnblogs.com/myfjd/p/2417335.html
Copyright © 2011-2022 走看看