实现文档下载,单个文件下载,和压缩包下载

实现文档下载

/**
     *
     * @return
     * @Author lrw
     * @Discription 下载文件:如果前端传过来的filePath为一个文件,就下载单个文件
     * 如果为多个文件,就下载一个压缩包文件
     */
    @Override
    public void downloadTranslationFiles(List<String> filePath, HttpServletResponse response) throws IOException {

        if (filePath.size() <= 1) {
            for (String fp : filePath) {
                downloadFilesForOne( fp, response);
            }
        } else {
            downloadFilesForZIP(filePath, response);
        }


    }

    /**
     * @param response
     * @throws IOException
     * @Author lrw
     * @Discription 下载单个文件
     */
    public void downloadFilesForOne(String filePath, HttpServletResponse response) throws IOException {
		
		
        FileSystemResource fileSystemResource = null;
        //文件输入流
        FileInputStream in = null;
		
        String filename = null;
        //字符输入流
        BufferedInputStream bis = null;

		
        filename = filePath.substring(filePath.lastIndexOf("\\") + 1);

		//设置响应头,如果没有 response 可以用 HttpHeader获得相应头
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        //通过文件地址获得文件资源
        fileSystemResource = new FileSystemResource(filePath);
        //通过文件资源获得文件    和上一步可以合并  通过文件地址获得文件
        File file = fileSystemResource.getFile();
		//获得响应输出流
        ServletOutputStream os = response.getOutputStream();
        //将文件放入输入流
        in = new FileInputStream(file);
        //将文件放入字节输入流
        bis = new BufferedInputStream(in, 1024 * 10);
        //设置一个缓冲池
        byte[] buf = new byte[1024 * 10];
        int len = 0;
        //读取bis  读取里面字符流
        while ((len = bis.read(buf, 0, 1024 * 10)) > 0) {
            //往输出流里写字符流
            os.write(buf, 0, len);
        }
        if (bis != null) {
            bis.close();
        }
        if (in != null) {
            in.close();
        }
        if (os != null) {`在这里插入代码片`
            os.close();
        }

    }

    /**
     * lrw
     * 下载ZIP
     * @param response
     * @throws IOException
     */
    public void downloadFilesForZIP(List<String> filePath, HttpServletResponse response) throws IOException {

        OutputStream os = null;
        FileSystemResource fileSystemResource = null;
        FileInputStream in = null;
        ZipOutputStream zos = null;
        String FilePath = null;
        String filename = null;
        BufferedInputStream bis = null;
        os = response.getOutputStream();

        zos = new ZipOutputStream(os);
        String zipFileName = "dataFile.zip";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, "UTF-8"));
        for (String fp : filePath) {

            FilePath = fp;
            filename = FilePath.substring(FilePath.lastIndexOf("\\") + 1);
            fileSystemResource = new FileSystemResource(FilePath);
            File file = fileSystemResource.getFile();
            //通过文件名  定义一个 zip文件的对象
            ZipEntry zipEntry = new ZipEntry(filename);
            zos.putNextEntry(zipEntry);
            in = new FileInputStream(file);
            bis = new BufferedInputStream(in, 1024 * 10);
            byte[] buf = new byte[1024 * 10];
            int len = 0;
            while ((len = bis.read(buf, 0, 1024 * 10)) > 0) {
                zos.write(buf, 0, len);
            }
            if (bis != null) {
                bis.close();
            }
            if (in != null) {
                in.close();
            }
            if (zos != null) {
                zos.closeEntry();
            }
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值