在jsp頁面產生二維碼,jsp頁面

來源:互聯網
上載者:User

在jsp頁面產生二維碼,jsp頁面

本文介紹線上產生二維碼功能。

思路仿照驗證碼產生的思路。

代碼如下:

package com.sys.productNew.action;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.huchi.util.qrutils.PictureUtils;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;public class ProductNewAction extends ActionSupport {// 產品編號private String productId;// 跳到二維碼頁面public String prevQRPicture() throws IOException {return Action.SUCCESS;}//產生預覽二維碼public void QRPicture() {HttpServletResponse response=ServletActionContext.getResponse();response.setDateHeader("Expires", 0);response.setHeader("Cache-Control", "no-cache");response.setHeader("Pragma", "No-cache");response.setContentType("image/jpeg");// 產生二維碼String url = "http://xx/yy/zz.action?productId=";BufferedImage image = PictureUtils.generateQRCode(url + productId);OutputStream os = null;try {os = response.getOutputStream();ImageIO.write(image, "JPEG", os);} catch (IOException e) {e.printStackTrace();} finally { if (os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}}}public String getProductId() {return productId;}public void setProductId(String productId) {this.productId = productId;}}


HTML :

<img alt="呼哧旅行" id="authCodeImage" src="/end/app/productNew/QRPicture.shtml?productId=${productId }"/>


該功能能夠將二維碼,以流的方式,寫在JSP頁面上。需要注意的是,通過<img>的src功能,訪問action裡面的方法,然後以流的方式,產生二維碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.