Java生成二维码和保存二维码(路径)

1.生成二维码

//生成二维码
	@RequestMapping("/genTDC")
	public ModelAndView genTDC(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setDateHeader("Expires", 0);
		// Set standard HTTP/1.1 no-cache headers.
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		// Set standard HTTP/1.0 no-cache header.
		response.setHeader("Pragma", "no-cache");
		// return a jpeg
		response.setContentType("image/jpeg");
		// create the text for the image
		String schema = request.getScheme();
		String servername = request.getServerName();
		InetAddress ip2 = InetAddress.getLocalHost();
		String localip = ip2.toString().substring(ip2.toString().indexOf("/") + 1);
		String path = request.getContextPath();
		int serverPort = request.getServerPort();
		// 根据wjid获取当前问卷时间以确定二维码有效期
		int sfyx = jspjService.queryewmyxq(wjid);//这个是判断二维码的条件 如果成立就能访问二维码 如果不成立就不能
		String capText1 = "二维码已失效";
		if (sfyx > 0) {
			// 获取ip
			// 根据URL获取当前项目二维码扫描到的地址 
			//这是本地的路径 http://ip:端口号/项目名/controller/方法
			//capText1 = schema + "://" + localip + ":" + serverPort + path + "/jspjj/toSweepCode.do“;
			//capText1 =”https://www.baidu.com/“;
		}
		ServletOutputStream out;
		try {
			out = response.getOutputStream();
			TwoDimensionCode.encoderQRCode(capText1, out, "jpg", 15);
		} catch (IOException e) {
			e.printStackTrace();
		};
		return null;
	}

直接访问这个路径就可以生成一张图片

2.生成并保存二维码

 //保存二维码到本地
	@RequestMapping("/saveImageDt")
	public ModelAndView saveImageDt(HttpServletRequest request, HttpServletResponse response)throws Exception {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");

		String schema = request.getScheme();
		String servername = request.getServerName();
		InetAddress ip2 = InetAddress.getLocalHost();
		String localip = ip2.toString().substring(ip2.toString().indexOf("/") + 1);
		String path = request.getContextPath();
		int serverPort = request.getServerPort();
			// 根据URL获取当前项目二维码扫描到的地址
			capText1 = ”“;
		response.addHeader("Content-Disposition", "attachment;filename=smdt.jpg");
		response.setContentType("image/jpeg");//保存的路径
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			TwoDimensionCode.encoderQRCode(capText1, os, "jpg", 15);
		} catch (Exception e) {
			e.printStackTrace();
		}
		InputStream is = null;
		OutputStream out = null;
		try {
			is = new ByteArrayInputStream(os.toByteArray());
			out = response.getOutputStream();
			byte[] b = new byte[1024];
			int i = 0;
			while ((i = is.read(b)) > 0) {
				out.write(b, 0, i);
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			is = null;
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			out = null;
		}
		return null;
	}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值