自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java+Vue導(dǎo)出zip壓縮包前后端實現(xiàn)

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

本例實現(xiàn)批量導(dǎo)出二維碼圖片文件,將所有的圖片放在一個zip壓縮包中。

實現(xiàn)步驟:

1、查詢數(shù)據(jù)循環(huán)生成二維碼圖片

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

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

3、前端Vue得到數(shù)據(jù)流實現(xiàn)下載

  • 調(diào)用后端接口,設(shè)置responseType: 'blob'
  • 通過window.navigator.msSaveBlob下載文件

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

/**
     * 導(dǎo)出二維碼
     *
     */
    @RequestMapping(value = "/exportQrcode")
    public void exportQrcode(HttpServletRequest request, HttpServletResponse response, ProQrcode proQrcode) throws IOException {
        // Step.1 組裝查詢條件
        // ... 此處省略數(shù)據(jù)查詢條件...
  // 查詢數(shù)據(jù)
        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);
    }
    /**
     * 批量生產(chǎn)圖片zip壓縮包數(shù)據(jù)
     * */
    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,設(shè)置文件名,后綴.png
                    zip.putNextEntry(new ZipEntry(String.format("%d.%s.png", i + 1, qrcode.getCode())));
                    // 查詢是否配置了logo,如果有l(wèi)ogo,則把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文件,設(shè)置響應(yīng)頭為文件下載
     */
    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設(shè)置HttpServletResponse響應(yīng)頭數(shù)據(jù),標(biāo)記為文件下載

二、Vue前端調(diào)用后端接口實現(xiàn)下載

/**
 * 導(dǎo)出二維碼數(shù)據(jù)
 */
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 = '導(dǎo)出文件'
  }
  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對象
  }
}

調(diào)用后端接口,設(shè)置responseType: 'blob'通過window.navigator.msSaveBlob下載文件


責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2024-11-27 08:34:53

ASPZIP壓縮包

2023-05-29 19:17:31

2023-03-29 08:59:59

Go壓縮包文檔

2023-05-18 22:34:15

2024-01-03 08:20:40

2011-12-30 11:14:41

Javazip

2016-12-14 09:24:42

文件目錄壓縮

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

2022-09-06 10:26:38

前后端分離Vue跨域

2023-02-08 16:29:58

前后端開發(fā)

2021-06-16 08:05:14

centos nginx 后端

2019-04-29 14:51:05

前后端JavaVue.js

2021-01-25 14:10:49

Spring BootVueJava

2018-10-23 14:24:10

2021-09-18 09:45:33

前端接口架構(gòu)

2021-01-04 21:00:53

開源軟件文件解壓開發(fā)者工具

2017-10-11 18:17:06

大數(shù)據(jù)數(shù)據(jù)可視化前后端
點贊
收藏

51CTO技術(shù)棧公眾號