批量下载,前后

  //后台代码

@RequestMapping(value = "/downloadInvoices", method = RequestMethod.GET)
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) // 数据的回滚,需要测试
    public void downloadInvoices(HttpServletResponse response, String ids)
            throws UnsupportedEncodingException, NoSuchAlgorithmException {
        //读取yml文件
        String address = env.getProperty("file.download.url");      //如果想指定下载位置 
        String uploadUrl = env.getProperty("file.url");        //上传时配置的路径
        try {
            List<String> paths = new ArrayList<>();
            if (ids.length()>0 && !"".equals(ids) && ids!=null){
                String[] splits = ids.split(",");
                for (String id : splits){
                    InvoiceFile invoiceFile = invoiceFileService.get(id);
                    String folder = invoiceFile.getFileId();        //文件夹的名称
                    String fileName = invoiceFile.getFileNum();     //文件名称
                    String filePath = uploadUrl + "/" + folder + "/" + fileName +".pdf";
                    System.out.println(filePath);
                    paths.add(filePath);

                }
            }

            String date = DateUtil.format(new Date(),"yyyyMMddHHmmss");
            if (paths.size() != 0) {
                File file = new File(address);
                if (!file.isDirectory()){
                    file.mkdir();
                }
//                String zipFilePath = address+"/批量下载"+date+".zip";     //本地生效
                String zipFilePath = "D:\\invoiceupload\\zip\\test.zip";   //部署宝塔上生效
             // 压缩输出流,包装流,将临时文件输出流包装成压缩流,将所有文件输出到这里,打成zip包
                ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
                for (String path:paths){
                    fileToZip(path,zipOut);        //这个抽取成方法,在下面
                }
                zipOut.close();     //关流
                //拼接下载默认名称并转为ISO-8859-1格式
                String fileName = new String(("批量下载"+date+".zip").getBytes(),"ISO-8859-1");
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attchment;filename="+fileName);
                //该流不可以手动关闭,手动关闭下载会出问题,下载完成后会自动关闭
                ServletOutputStream outputStream = response.getOutputStream();
                FileInputStream inputStream = new FileInputStream(zipFilePath);
                // copy方法为文件复制,在这里直接实现了下载效果
//引入这个包下面的import org.apache.tomcat.util.http.fileupload.IOUtils;
                IOUtils.copy(inputStream, outputStream);
                inputStream.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

public static void fileToZip(String filePath,ZipOutputStream zipOut) throws IOException {
    // 需要压缩的文件
    File file = new File(filePath);
    // 获取文件名称,如果有特殊命名需求,可以将参数列表拓展,传fileName
    String fileName = file.getName();
    FileInputStream fileInput = new FileInputStream(filePath);
    // 缓冲
    byte[] bufferArea = new byte[1024 * 10];
    BufferedInputStream bufferStream = new BufferedInputStream(fileInput, 1024 * 10);
    // 将当前文件作为一个zip实体写入压缩流,fileName代表压缩文件中的文件名称
    zipOut.putNextEntry(new ZipEntry(fileName));
    int length = 0;
    // 最常规IO操作,不必紧张
    while ((length = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) {
        zipOut.write(bufferArea, 0, length);
    }
    //关闭流
    fileInput.close();
    // 需要注意的是缓冲流必须要关闭流,否则输出无效
    bufferStream.close();
    // 压缩流不必关闭,使用完后再关
}

//  前台

// 批量下载
function downloadInvoice() {
    var checkStatus = table.checkStatus('invoice'),
        data = checkStatus.data,
        ids="";
    if(data.length > 0) {
        for (var i in data) {
            if(ids.length>0){
                ids=ids+","
            }
            ids=ids+data[i].id;
        }
        layer.confirm('确定批量下载选中的发票?', {icon: 3, title: '提示信息'}, function (index) {
        //这种直接在浏览器底部出现
            location.href = invoiceDownloadUrl+"?ids="+ids;
            layer.close(index);
        })
    }else{
        layer.msg("请选择需要下载的发票",{  time: 2000 },function () {});
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

my_name_is_sky

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值