Java 批量生成二维码图片,并打包成可直接下载的zip包(spring boot)

最近做的一个项目中,需要提供给web端一个接口:downloadQr。用到了Google的Zxing框架,版本是3.3.1 。项目使用的是springboot框架。

一、API文档

接口名称:/downloadAllQr
接口描述:批量生成二维码图片,并打包成可直接下载的zip包
请求方式:GET
请求参数:String data
响应内容:zip文件流

二、下面直接上代码

  • controller层
import com.google.zxing.WriterException;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@GetMapping("downloadAllQr")
    public void downloadAllQr(HttpServletRequest httpServletRequest, HttpServletResponse      httpServletResponse, @RequestParam("data") String data) throws IOException, WriterException {
       biz.downloadAllQr(httpServletRequest, httpServletResponse, data);
    }
  • biz层 interface
import com.google.zxing.WriterException;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

void downloadAllQr(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String data) throws WriterException, IOException;
  • biz层 实现
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.IntrospectionException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Override
    public void downloadAllQr(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String data)
            throws WriterException, IOException {
        httpServletResponse.setContentType("application/zip");
            httpServletResponse.setHeader("Content-disposition",
                    "attachment; filename=" + new String(PoiUtil.getFileName("二维码列表").getBytes(), 
                    "ISO-8859-1") + ".zip");

            OutputStream outputStream = httpServletResponse.getOutputStream();
            ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);

            for (DbDO row : dbDOS) {
                BitMatrix bitMatrix = new MultiFormatWriter().encode(data + row.getId(), BarcodeFormat.QR_CODE, Constant.QR_WIDTH, Constant.QR_HEIGHT);
                BufferedImage buffImg = MatrixToImageWriter.toBufferedImage(bitMatrix);

                ZipEntry entry = new ZipEntry(row.getTitle() + "." + GlobalBizImpl.QR_FILE_TYPE);
                zipOutputStream.putNextEntry(entry);
                ImageIO.write(buffImg, GlobalBizImpl.QR_FILE_TYPE, zipOutputStream);
                zipOutputStream.flush();
            }

            zipOutputStream.close();

            outputStream.flush();
            outputStream.close();
    }
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页