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();

 

posted @ 2020-08-24 09:22  打代码的小超人  阅读(434)  评论(0编辑  收藏  举报