我们用到了二维码所需要的依赖包,及hutool工具类
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.21</version>
</dependency>
案例
@ApiOperation("招生海报-生成二维码")
public void getShareImg(HttpServletResponse response) throws IOException {
String url = "https://blog.csdn.net/qq_42281649?spm=1000.2115.3001.5343";
BufferedImage image = QrCodeUtil.generate(url, 300, 300);
//输出流
String base64 = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("海报","UTF-8") + ".jpg");
//编码
response.setCharacterEncoding("UTF-8");
ImageIO.write(image, "jpg", response.getOutputStream());
}
另附二维码