java生成二维码

Posted 红尘沙漏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java生成二维码相关的知识,希望对你有一定的参考价值。

jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String codePath=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();

String user_no=(String)request.getSession().getAttribute("usr_no");
String typeStr = (String)request.getSession().getAttribute("typ");


%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="<%=basePath%>css/themes/crud.css" rel="stylesheet" type="text/css"/>

<meta charset="UTF-8">
<title>Insert title here</title>
</head>

<body>
<div align="center">
<img height="250px" style="padding:5px 5px;margin-top:10%;" width="250px" id="file_path1" name="file_path1" src="<%=basePath%>css/img/ewm.jpg"/>
</div>
<div style="margin:auto;margin-top:5%;">
<span style="text-align: center;display:block;">
<!-- <a href="javascript:void(0)" class="easyui-linkbutton" onclick="createCode()" id="create">生成二维码</a>
<a href="javascript:void(0)" id="down" class="easyui-linkbutton" onclick="downCode()">下载二维码</a> -->
<button type="button" onclick="createCode()" id="create" style="width:100px;height:34px">生成二维码</button>
</span>
</div>
<script type="text/javascript">
$(function(){
<%-- $("#down").hide();
$.ajax({
url: "<%=basePath%>xyData/qrcode/imgNeed.do",
data: ‘‘,
type: ‘POST‘,
cache: false,
dataType: ‘json‘,
success: function (data) {
if(data.success){
$("#file_path1").attr(‘src‘,‘<%=basePath%>xyData/qrcode/findQrcode.do‘);
}else{
$("#file_path1").attr(‘src‘,‘<%=basePath%>css/images/ewm.jpg‘);

}
} ,
error: function () {
$.messager.show({
title:‘提示‘,
msg: data.msg,
showType:‘show‘
});
}
}); --%>


});
var real_path;
function createCode(){
$.ajax({
url: "<%=basePath%>qrcode/createQrCode.do",
data: ‘‘,
type: ‘POST‘,
cache: false,
dataType: ‘json‘,
success: function (data) {
var name=data.name;
real_path = data.path;
//var url=data+"?s="+ (new Date()).getTime();
document.getElementById("file_path1").src ="<%=basePath%>qrcode/"+name+".jpg";
//$("#file_path1").attr("src",url);
},
error: function () {
$.messager.show({
title:‘提示‘,
msg: data.msg,
showType:‘show‘
});
}
});
}
function downCode(){
alert(real_path);
downCode1(real_path);
}
function downCode1(real_path){
window.location.href="<%=basePath %>file/download.do?path="+real_path;
}

</script>

</body>

</html>

java代码=====

package controller.qrcode;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bean.QrCodeEntity;
import serviceimpl.impl.QrCodeServiceImpl;
import utile.QRCodeUtil;
import utile.ReadImageUtils;

@Controller
@RequestMapping("/qrcode")
public class QrCodeController {
@Autowired
private QrCodeServiceImpl qrCodeServiceImpl;
@RequestMapping("/createQrCode.do")
@ResponseBody
public Map createQrCode(HttpServletRequest request,HttpServletResponse response) {
Map<String,Object>map=new HashMap<String,Object>();
InputStream in=null;
QrCodeEntity qrCodeEntity=new QrCodeEntity();
String text="http://192.168.1.75:8082/xycc-Web-app/user/getCompanyByCompanyidAndUserNo.do?company_id=xxxx&user_no=yyyy&flag=down";

String user_no=(String)request.getSession().getAttribute("user_no");
String user_name=(String)request.getSession().getAttribute("user_name");
System.out.println("user_no=="+user_no+"--user_name=="+user_name+"--text==="+text);
String conpany_id="pei000100105555";
text=text.replace("xxxx", user_no).replace("yyyy", user_name);
System.out.println("text==="+text);
try {
String name=String.valueOf(Math.random());
String destPath=getFilepath()+name+".jpg";
System.out.println("destPath=="+destPath);
//生成二维码
QRCodeUtil.encode(text, destPath);
//存入数据库
String photoName=name+".jpg";
in= ReadImageUtils.getIns(destPath);
System.out.println("photoName==="+photoName);
System.out.println("in=="+in);

long start=System.currentTimeMillis();
String s= UUID.randomUUID().toString().replaceAll("-", "");
qrCodeEntity.setId(start+s.substring(14,18)+s.substring(19, 23)+s.substring(24));
qrCodeEntity.setPhotoName(photoName);
qrCodeEntity.setPhotoBlob(InputStreamToByte(in));
qrCodeEntity.setCompany_id(conpany_id);
qrCodeEntity.setUsr_no(user_no);
qrCodeServiceImpl.saveQrcode(qrCodeEntity);
map.put("success", true);
map.put("msg", "生成二维码成功");
map.put("name",name);
map.put("path",destPath);
map.put("photoBlob",qrCodeEntity.getPhotoBlob());
} catch (Exception e) {
map.put("success", false);
map.put("msg", "生成二维码失败");
e.printStackTrace();
// TODO: handle exception
}
return map;
}
public byte[]InputStreamToByte(InputStream iStrm) throws IOException{
ByteArrayOutputStream bytestream=new ByteArrayOutputStream();
int ch;
while((ch=iStrm.read())!=-1) {
bytestream.write(ch);
}
byte imgdata[]=bytestream.toByteArray();
bytestream.flush();
bytestream.close();
return imgdata;
}
//获取文件路径
public String getFilepath() throws FileNotFoundException {
File path=new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) {
path=new File("");
}
File upload=new File(path.getAbsolutePath(),"file/qrcode/");
if(!upload.exists()) {
upload.mkdirs();
}
System.out.println("upload-url="+upload.getAbsolutePath()+"/");
return upload.getAbsolutePath()+"/";
}

}

实体类=====

package bean;

import java.io.Serializable;

@SuppressWarnings("serial")
public class QrCodeEntity implements Serializable{

private String id;
private String photoName;
private byte[] photoBlob;
private String usr_no;
private String company_id;
public String getId(){
return this.id;
}
public String getUsr_no() {
return usr_no;
}
public void setUsr_no(String usr_no) {
this.usr_no = usr_no;
}
public String getCompany_id() {
return company_id;
}
public void setCompany_id(String conpany_id) {
this.company_id = conpany_id;
}
public void setId(String id){
this.id=id;
}
public String getPhotoName(){
return this.photoName;
}
public void setPhotoName(String photoName){
this.photoName=photoName;
}
public byte[] getPhotoBlob() {
return photoBlob;
}
public void setPhotoBlob(byte[] photoBlob) {
this.photoBlob = photoBlob;
}



}

实现类

package serviceimpl.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bean.QrCodeEntity;
import dao.QrCodeDao;
import service.QrCodeService;

@Service
public class QrCodeServiceImpl implements QrCodeService{
@Autowired
private QrCodeDao qrCodeDao;
/**
* 保存二维码生成的参数
* @param qrCodeEntity
*/
@Override
public void saveQrcode(QrCodeEntity qrCodeEntity) {
qrCodeDao.saveQrcode(qrCodeEntity);

}

}

以上是关于java生成二维码的主要内容,如果未能解决你的问题,请参考以下文章

java使用qcord生成的二维码,手机可以扫,扫描枪扫不出来,或者很难扫出来?

java 生产二维码报错

java 生成 二维码 和jquery 生成二维码

java实现生成二维码

如何使用java开发二维码代码

Java生成二维码