成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Java+Vue導出zip壓縮包前后端實現

開發 前端
通過cn.hutool.extra.qrcode.QrCodeUtil生成二維碼圖片,得到byte[]通過java.util.zip.ZipOutputStream將byte[]寫入壓縮包通過java.io.ByteArrayOutputStream返回完整的byte[]全部寫入完成,得到完整的byte[]輸出到HttpServletResponse設置HttpServletResponse響應。

本例實現批量導出二維碼圖片文件,將所有的圖片放在一個zip壓縮包中。

實現步驟:

1、查詢數據循環生成二維碼圖片

2、將生成的二維碼圖片放在一個壓縮包中,通過數據流返回給前端

  • 通過cn.hutool.extra.qrcode.QrCodeUtil生成二維碼圖片,得到byte[]
  • 通過java.util.zip.ZipOutputStream將byte[]寫入壓縮包
  • 通過java.io.ByteArrayOutputStream返回完整的byte[]
  • 全部寫入完成后,得到完整的byte[]輸出到HttpServletResponse
  • 設置HttpServletResponse響應頭數據,標記為文件下載

3、前端Vue得到數據流實現下載

  • 調用后端接口,設置responseType: 'blob'
  • 通過window.navigator.msSaveBlob下載文件

一、后端接口生成zip壓縮文件byte[]

/**
     * 導出二維碼
     *
     */
    @RequestMapping(value = "/exportQrcode")
    public void exportQrcode(HttpServletRequest request, HttpServletResponse response, ProQrcode proQrcode) throws IOException {
        // Step.1 組裝查詢條件
        // ... 此處省略數據查詢條件...
  // 查詢數據
        List<ProQrcode> list = service.list(queryWrapper);
        int width = 800;
        if (StringUtils.isNotBlank(widthStr)) {
            width = Integer.parseInt(widthStr);
        }
        byte[] data = genQrcodeImg(list, width);
        zip(response, data);
    }
    /**
     * 批量生產圖片zip壓縮包數據
     * */
    private byte[] genQrcodeImg(List<ProQrcode> list, int width) {
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
             ZipOutputStream zip = new ZipOutputStream(outputStream)) {

            for (int i = 0; i < list.size(); i++) {
                ProQrcode qrcode = list.get(i);
                try {
                    // 添加到zip,設置文件名,后綴.png
                    zip.putNextEntry(new ZipEntry(String.format("%d.%s.png", i + 1, qrcode.getCode())));
                    // 查詢是否配置了logo,如果有logo,則把logo添加到二維碼中
                    BufferedImage logo = CustomerBrandCache.getLogo(qrcode.getCustomerBrandId());
                    QrConfig config = new QrConfig();
                    config.setWidth(width).setHeight(width);
                    if (logo != null) {
                        config.setImg(logo);
                    }
                    // 生成二維碼圖片
                    byte[] bytes = QrCodeUtil.generatePng(qrcode.getLinkUrl(), config);
                    // 將byte[]寫入到壓縮包中
                    IOUtils.write(bytes, zip);
                    zip.flush();
                    zip.closeEntry();
                } catch (IOException e) {
                    log.error("addQrcode,error:", e);
                }
            }
            return outputStream.toByteArray();
        } catch (Exception e) {
            log.error("", e);
        }
        return new byte[0];
    }

    /**
     * 生成zip文件,設置響應頭為文件下載
     */
    private void zip(HttpServletResponse response, byte[] data) throws IOException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"qrcode.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

通過cn.hutool.extra.qrcode.QrCodeUtil生成二維碼圖片,得到byte[]通過java.util.zip.ZipOutputStream將byte[]寫入壓縮包通過java.io.ByteArrayOutputStream返回完整的byte[]全部寫入完成后,得到完整的byte[]輸出到HttpServletResponse設置HttpServletResponse響應頭數據,標記為文件下載

二、Vue前端調用后端接口實現下載

/**
 * 導出二維碼數據
 */
export const exportQrcode = async (name, params) => {
  const data = await defHttp.get({ url: Api.exportQrcode, params, responseType: 'blob', timeout: 30000 }, { isTransformResponse: false })
  if (!data) {
    createMessage.warning('文件下載失敗')
    return
  }
  if (!name || typeof name != 'string') {
    name = '導出文件'
  }
  const blobOptions = { type: 'application/octet-stream' }
  const fileSuffix = '.zip'
  debugger
  if (typeof window.navigator.msSaveBlob !== 'undefined') {
    window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix)
  } else {
    const url = window.URL.createObjectURL(new Blob([data], blobOptions))
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', name + fileSuffix)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link) //下載完成移除元素
    window.URL.revokeObjectURL(url) //釋放掉blob對象
  }
}

調用后端接口,設置responseType: 'blob'通過window.navigator.msSaveBlob下載文件


責任編輯:武曉燕 來源: 今日頭條
相關推薦

2024-11-27 08:34:53

ASPZIP壓縮包

2023-05-29 19:17:31

2023-03-29 08:59:59

Go壓縮包文檔

2024-01-03 08:20:40

2023-05-18 22:34:15

2016-12-14 09:24:42

文件目錄壓縮

2011-12-30 11:14:41

Javazip

2024-02-27 08:27:18

元素拖拽Vue3拼圖驗證

2022-04-06 07:50:57

JWT后端Spring

2019-06-12 19:00:14

前后端分離AppJava

2022-09-01 07:18:21

分離項目Vue

2023-02-08 16:29:58

前后端開發

2022-09-06 10:26:38

前后端分離Vue跨域

2021-06-16 08:05:14

centos nginx 后端

2019-04-29 14:51:05

前后端JavaVue.js

2021-01-25 14:10:49

Spring BootVueJava

2021-09-18 09:45:33

前端接口架構

2018-10-23 14:24:10

2021-01-04 21:00:53

開源軟件文件解壓開發者工具

2017-10-11 18:17:06

大數據數據可視化前后端
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美激情视频一区二区三区免费 | 亚洲一区成人 | 久草视频在线播放 | 国产精品美女久久久久久免费 | 久久久久久国产 | 第四色影音先锋 | 在线观看国产91 | 玖玖视频国产 | 爱爱综合网 | 色综合色综合色综合 | 91精品久久久 | 免费99精品国产自在在线 | 亚洲一区二区三区高清 | 久久se精品一区精品二区 | 中文字幕久久精品 | 免费在线精品视频 | 国产成人免费视频 | 国产精品一区二区av | av网站在线免费观看 | 波多野结衣一二三区 | 亚洲国产精品成人综合久久久 | 欧美性生活网 | 欧美日韩专区 | 亚洲欧美视频一区 | 国产 欧美 日韩 一区 | 亚洲一区亚洲二区 | 国产精品综合网 | 91www在线观看 | 精品一级 | 国产一级在线 | 日韩一二区 | 围产精品久久久久久久 | 国产精品国产成人国产三级 | 国产精品成人品 | 国产日韩一区二区三区 | 久久天天| 韩国久久精品 | 亚洲成人动漫在线观看 | 日韩91在线| 国产一区二区三区久久久久久久久 | 最近中文字幕在线视频1 |