Java 生成可直接下载的二维码(spring boot)

最近做的一个项目中,需要提供给web端一个接口:downloadQr。用到了Google的Zxing框架。项目使用的是springboot框架。一、API文档接口名称:/downloadQr 接口描述:生成可直接下载的二维码图片 请求方式:GET 请求参数:String data 响应内容:二维码图片数据流二、下面直接上代码controller层@GetMapping("download
摘要由CSDN通过智能技术生成

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

一、API文档

接口名称:/downloadQr
接口描述:生成可直接下载的二维码图片
请求方式:GET
请求参数:String data
响应内容:二维码图片数据流

二、下面直接上代码
  • 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.IOE
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Java生成二维码下载的代码示例: ```java // 导入相关包 import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @Controller @RequestMapping("/qrcode") public class QRCodeController { @GetMapping("/download") @ResponseBody public void downloadQRCode(@RequestParam("content") String content, HttpServletResponse response) throws IOException, WriterException { // 设置二维码参数 int width = 300; // 二维码宽度 int height = 300; // 二维码高度 String format = "png"; // 二维码图片格式 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 编码格式 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 容错等级 hints.put(EncodeHintType.MARGIN, 1); // 边距 // 生成二维码 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); // 将二维码图片写入文件 File file = new File("qrcode.png"); ImageIO.write(image, format, file); // 设置响应头,使浏览器能够下载文件 response.setHeader("Content-Disposition", "attachment;filename=qrcode.png"); response.setContentType("application/octet-stream"); response.setContentLength((int) file.length()); // 将文件写入响应流 FileInputStream in = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } in.close(); out.close(); } } ``` 以上代码使用Spring Boot框架,通过调用`/qrcode/download`接口,传入二维码内容`content`,即可生成二维码下载到本地。其中,二维码参数可以根据需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值