下载

来源:互联网 发布:江西财经大学数据库 编辑:程序博客网 时间:2024/04/30 03:12
package com.capinfo.common.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import com.capinfo.common.action.BaseAction;

public class FileAction extends BaseAction {

    @Override
    public String list() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }
    private HttpServletResponse response;

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public String getFileName() {
        return fileName;
    }

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

        String mPath ="D:\\";

        //mPath 也可以以读取配置文件的方式进行提取,这里就不再重复了。

        String filePath =mPath+fileName;

        downloadFileRanges(filePath);
        return null;

}



/**
     * 断点续传的方式进行下载
     * @param filePath 带文件名的绝对路径
     * @return
     */
    private String downloadFileRanges(String filePath) {

        System.out.println("下载时,收到传进来的值:" + filePath);
        System.out.println(filePath);

        File file = new File(filePath);
        if (!file.exists()) {
            // 文件不存在
            return null;
        } else {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);

                this.response.setHeader("Accept-Ranges", "bytes");
                long p = 0;
                long l = 0;
                // l = raf.length();
                l = file.length();
                // 如果是第一次下,还没有断点续传,状态是默认的 200,无需显式设置
                // 响应的格式是:
                // HTTP/1.1 200 OK
                if (this.request.getHeader("Range") != null) // 客户端请求的下载的文件块的开始字节
                {
                    // 如果是下载文件的范围而不是全部,向客户端声明支持并开始文件块下载
                    // 要设置状态
                    // 响应的格式是:
                    // HTTP/1.1 206 Partial Content
                    this.response
                            .setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);// 206
                    // 从请求中得到开始的字节
                    // 请求的格式是:
                    // Range: bytes=[文件块的开始字节]-
                    p = Long.parseLong(this.request.getHeader("Range")
                            .replaceAll("bytes=", "").replaceAll("-", ""));
                }
                // 下载的文件(或块)长度
                // 响应的格式是:
                // Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节]
                this.response.setHeader("Content-Length", new Long(l - p)
                        .toString());
                if (p != 0) {
                    // 不是从最开始下载,
                    // 响应的格式是:
                    // Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
                    this.response.setHeader("Content-Range", "bytes "
                            + new Long(p).toString() + "-"
                            + new Long(l - 1).toString() + "/"
                            + new Long(l).toString());
                }
                // response.setHeader("Connection", "Close"); //如果有此句话不能用 IE
                // 直接下载
                // 使客户端直接下载
                // 响应的格式是:
                // Content-Type: application/octet-stream
                this.response.setContentType("application/octet-stream");
                // 为客户端下载指定默认的下载文件名称
                // 响应的格式是:
                // Content-Disposition: attachment;filename="[文件名]"
                // response.setHeader("Content-Disposition",
                // "attachment;filename=\"" + s.substring(s.lastIndexOf("\\") +
                // 1) +
                // "\""); //经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉
                // FileInputStream 版本的语句
                this.response.setHeader("Content-Disposition",
                        "attachment;filename=" + file.getName() + "");
                // raf.seek(p);
                fis.skip(p);
                byte[] b = new byte[1024];
                int i;
                // while ( (i = raf.read(b)) != -1 ) //经测试 RandomAccessFile
                // 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句
                while ((i = fis.read(b)) != -1) {
                    this.response.getOutputStream().write(b, 0, i);
                    this.response.flushBuffer();
                }
                // raf.close();//经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉
                // FileInputStream 版本的语句
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                        this.response.getOutputStream().close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
        return null;

    }

}

0 0