com.yixan.base.web.controller.QrCodeLinkImageController Maven / Gradle / Ivy
package com.yixan.base.web.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.yixan.tools.common.util.QrCode;
import com.yixan.tools.common.util.StringUtil;
import com.yixan.tools.common.util.config.Config;
/**
* 二维码图片
* 示例: qrcode/link/detail/xxx/10001.png
* 配置: site.manager.outer.url = http://www.abc.com/m/
* 配置: qrcode.xxx = {config:site.manager.outer.url}xxx/{id}.html
* 结果: 返回二维码图片, 扫描结果为http://www.abc.com/m/xxx/10001.html
*
* @author zhaohuihua
* @version C01 2017-07-04
*/
@Controller
@RequestMapping("qrcode/link")
public class QrCodeLinkImageController {
/** 日志对象 **/
private static final Logger log = LoggerFactory.getLogger(QrCodeLinkImageController.class);
@Autowired
private Properties setting;
@RequestMapping(value = "detail/{type}/{id}.png", method = RequestMethod.GET)
public void qrcode(@PathVariable("type") String type, @PathVariable("id") String id, HttpServletRequest request,
HttpServletResponse response) {
try {
// 根据类型读取配置项
String string = Config.getRealValue(setting, "qrcode." + type, true);
String content = StringUtil.format(string, "id", id);
// 生成二维码并输出
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
QrCode.generate(content, 200, 0, os);
os.flush();
os.close();
} catch (IOException e) {
log.error("Failed to generate qr code, type={}, id={}", type, id, e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy