前臺一個標籤: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