在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里面的方法,然后以流的方式,生成二维码。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值