日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java+Vue導(dǎo)出zip壓縮包前后端實(shí)現(xiàn)

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

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到桂平網(wǎng)站設(shè)計(jì)與桂平網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋桂平地區(qū)。

實(shí)現(xiàn)步驟:

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

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

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

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

  • 調(diào)用后端接口,設(shè)置responseType: 'blob'
  • 通過(guò)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 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 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());
    }

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

二、Vue前端調(diào)用后端接口實(shí)現(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對(duì)象
  }
}

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


網(wǎng)站欄目:Java+Vue導(dǎo)出zip壓縮包前后端實(shí)現(xiàn)
文章出自:http://www.5511xx.com/article/dhchdhj.html