前台一个标签:html
<img src="${encodedContextPath}/test/image" >
就一个请求到后台的地址.java
后台代码:app
@RequestMapping(value = "/image") public void image(HttpServletResponse response){ try { String codeUrl="https://www.baidu.com/"; BufferedImage bufferedImage = code2dService.encodeQrCode(codeUrl, Config.getInt("wechat.qrcode.width", 5), Config.getInt("wechat.qrcode.height", 5)); // File file=File.createTempFile("qrcode"+System.currentTimeMillis(), ".png"); // ImageIO.write(bufferedImage, "png", file); ImageIO.write(bufferedImage, "png", response.getOutputStream()); response.setContentType("image/png"); response.flushBuffer(); } catch (Exception e) { e.printStackTrace(); LOG.error("transform image stream error",e); } }
这里只是个例子,能够自行弄个流返回就行.代码里面 code2dService 这个是我本地的.code
另外注释那行代码是生成一个png图片出来.orm