生成二维码并下载

文章目录
  1. 1. 主要代码
  2. 2. 生成二维码类和jar包下载

公司有个需求需要生成,下载二维码,供扫码快速完成操作。

主要代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 生成,下载二维码
*
* @param aoData
* @return
*/
@RequestMapping("load")
public void load(String fromWhere, String url, HttpServletResponse response, HttpServletRequest request, HttpSession session) throws IOException {
BufferedInputStream dis = null;
BufferedOutputStream fos = null;
try {
UUID uuid = UUID.randomUUID();
//response.setContentType("application/x-msdownload;");
String fileName = uuid+".jpg";
String content = url + "?fromWhere=" + fromWhere;
//获取文件的路径
String ccbPath = session.getServletContext().getRealPath("/") + "assets/images/logo.png" ;
System.out.println(ccbPath);
InputStream qrcode = EncoderHandler.encoderQRCoder(content, ccbPath);
response.setContentType("image/png");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
byte[] byt = new byte[qrcode.available()];
response.setHeader("Content-Length", String.valueOf(byt.length));
dis = new BufferedInputStream(qrcode);
fos = new BufferedOutputStream(response.getOutputStream());

byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = dis.read(buff, 0, buff.length))) {
fos.write(buff, 0, bytesRead);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (dis != null)
dis.close();
if (fos != null)
fos.close();
}
}

生成二维码类和jar包下载

[upl-file uuid=9ee7b502-44cc-4612-bb78-f128a3119625 size=1MB]qrcode-tools.zip[/upl-file]

评论