• java生成二维码给浏览器下载


    maven 配置

    <!--google生成二维码-->
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>core</artifactId>
                <version>3.3.0<version>
            </dependency>
            <dependency>
                <groupId>com.google.zxing</groupId>
                <artifactId>javase</artifactId>
                <version>3.3.0<version>
            </dependency>

    Controller代码 将多张图片打包成zip下载

    package com.example.abc.controller.lx;

    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.WriterException;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.QRCodeWriter;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;

    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    import java.nio.file.FileSystems;
    import java.nio.file.Path;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;

    @Controller
    @RequestMapping("/lx")
    public class LxController {

    @RequestMapping("/filerar")
    @ResponseBody
    public HttpServletResponse fileRar(HttpServletResponse response) throws Exception {
    String qrCode[] = {"123456", "15135", "13155", "164513"};
    // FileOutputStream fileOutputStream = null;

    String fileZipName = "111";
    // 配置文件下载
    response.setHeader("content-type", "application/octet-stream");
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename="+ fileZipName +".zip");

    // 获取二维码数组集合
    List<byte[]> list = createQrCode(qrCode);

    //将文件打包成压缩文件
    ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
    ZipEntry zipEntry = null;
    for (int i = 0; i < qrCode.length; i++) {
    zipEntry = new ZipEntry(qrCode[i] + ".png");
    zipOut.putNextEntry(zipEntry);
    zipOut.write(list.get(i));
    }

    zipOut.closeEntry();
    zipOut.close();

    return response;
    }

    public static List<byte[]> createQrCode(String qrCode[]) throws Exception{
    List<byte[]> list = new ArrayList<>();
    QRCodeWriter qrCodeWriter = new QRCodeWriter();

    for (String text : qrCode) {
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 350, 350);
    ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
    MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
    byte[] pngData = pngOutputStream.toByteArray();
    list.add(pngData);
    }

    return list;
    }

    }

    //单张图片下载

    ExtensionDetailInfo detail = detailList.get(0);
    //图片名称
    String name= detail.getRecommenderId()+"-"+detail.getRecommenderName();
    //二维码中包含的信息
    String content = url+"extensionId="+detail.getExtensionId()+"&packId="+detail.getPackId()+"&recommenderId="+detail.getRecommenderId()+"&relationRole="+URLEncoder.encode( detail.getRelationRole(),"UTF-8");
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    // 指定编码格式
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    // 编码内容,编码类型(这里指定为二维码),生成图片宽度,生成图片高度,设置参数
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
    //设置请求头
    response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name+".png", "UTF-8"));
    response.setHeader("Content-Type","application/octet-stream");
    OutputStream outputStream = response.getOutputStream();
    MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
    outputStream.flush();
    outputStream.close();
  • 相关阅读:
    Hall定理
    c#汉字转为拼音
    asp.net导出Excel 按照预定格式,以及解决导出乱码
    asp.net导出Excel
    SQL正常工作日上班安排
    SQL做日历
    T-SQL数组循环
    存储过程删除 用于更改,
    数组操作-去除重复和空白元素
    TreeView递归取值
  • 原文地址:https://www.cnblogs.com/feathe/p/13552025.html
Copyright © 2020-2023  润新知