码迷,mamicode.com
首页 > 编程语言 > 详细

java生成二维码

时间:2019-12-08 10:40:01      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:cli   type   dep   app   onclick   tty   hot   location   start   

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生成二维码

标签:cli   type   dep   app   onclick   tty   hot   location   start   

原文地址:https://www.cnblogs.com/xianz666/p/12004704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!