二维码生成

controller

1 public void getTrainSignQRCode(QRCodeDto qrCodeDto, HttpServletRequest request, HttpServletResponse response) throws Exception {
2         //拼接参数
3         HashMap<String, Object> map = new HashMap<>();
4         map.put("id",qrCodeDto.getId());
5         map.put("source",qrCodeDto.getSource());
6         String content = JSON.toJSONString(map);
7         commonService.getTrainSignQRCode(content, request, response);
8       
9     }

impl

 1 public void getTrainSignQRCode(String content, HttpServletRequest request, HttpServletResponse response) throws Exception {
 2         //二维码中包含的信息
 3         Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
 4         // 指定编码格式
 5         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
 6 
 7         //下面是毫秒级的时间戳
 8         String tempPath = name.format(new Date());
 9         // 获取文件路径  本地使用
10         String path = System.getProperty("user.dir");
11         String url = path+"/qrcode";
12         File pathFile = new File(path+"/qrcode"+File.separator+tempPath);
13         // 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
14         hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
15         // try {
16         // 编码内容,编码类型(这里指定为二维码),生成图片宽度,生成图片高度,设置参数
17         BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
18         //设置请求头
19         response.setCharacterEncoding("UTF-8");
20         response.setHeader("Content-Type", "application/octet-stream");
21         //设置输出时的名称
22         response.setHeader("Content-Disposition", "attachment;filename=" + tempPath + ".png");
23         String name = pathFile + ".png";
24         Path file = new File(name).toPath();
25         MatrixToImageWriter.writeToPath(bitMatrix, "png", file);
26         QRCode.ioReadImage(url,file.toString(), response);
27     
 1  public static void ioReadImage(String url,String imgUrl, HttpServletResponse response) throws Exception {
 2         ServletOutputStream out = null;
 3         FileInputStream ips = null;
 4         try {
 5             String imgPath = imgUrl;
 6             ips = new FileInputStream(new File(imgPath));
 7             String type = imgUrl.substring(imgUrl.indexOf(".") + 1);
 8             if (type.equals("png")) {
 9                 response.setContentType("image/png");
10             }
11             if (type.equals("jpeg") || type.equals("jpg")) {
12                 response.setContentType("image/jpeg");
13             }
14             out = response.getOutputStream();
15             //读取文件流
16             int len = 0;
17             byte[] buffer = new byte[1024 * 10];
18             while ((len = ips.read(buffer)) != -1) {
19                 out.write(buffer, 0, len);
20             }
21             out.flush();
22 
23         } catch (Exception e) {
24             e.printStackTrace();
25         } finally {
26             out.close();
27             ips.close();
28             delAllFile(url);
29         }
30     }

 

 1     //删除指定文件夹下所有文件
 2 //param path 文件夹完整绝对路径
 3     public static boolean delAllFile(String path) {
 4         boolean flag = false;
 5         File file = new File(path);
 6         if (!file.exists()) {
 7             return flag;
 8         }
 9         if (!file.isDirectory()) {
10             return flag;
11         }
12         String[] tempList = file.list();
13         File temp = null;
14         for (int i = 0; i < tempList.length; i++) {
15             if (path.endsWith(File.separator)) {
16                 temp = new File(path + tempList[i]);
17             } else {
18                 temp = new File(path + File.separator + tempList[i]);
19             }
20             if (temp.isFile()) {
21                 temp.delete();
22             }
23             if (temp.isDirectory()) {
24                 delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
25                 delFolder(path + "/" + tempList[i]);//再删除空文件夹
26                 flag = true;
27             }
28         }
29         return flag;
30     }

 

posted on 2020-05-16 18:31  好名字被谁用了  阅读(217)  评论(0编辑  收藏  举报

导航