二维码写入到数据库
1,数据库的字段用BLOB接受 hibernate映射blob2,存储二维码public static Blob StorageTwoDimensionCode(TbAssets assets) throws IOException{ByteArrayOutputStream outputStream = new ByteArrayOut
·
1,数据库的字段用BLOB接受 hibernate映射blob
2,存储二维码
2,存储二维码
public static Blob StorageTwoDimensionCode(TbAssets assets) throws IOException{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();//字节数组流
code.encoderQRCode(content, outputStream, "png",9);
//这里的Blob是java.sql包中的
Blob codeimg = Hibernate.createBlob(outputStream.toByteArray()); //outputStream转一个blob,这里可以接收InputStream和byte[]
outputStream.flush();
outputStream.close();
return codeimg; //返回一个blob
}
3,如果要读出来,在页面显示
public void showCodeImg(){
TbAssets assets = assetsService.getAssetsById(asid);
Blob codeimg = assets.getCodeImg();
try {
BufferedInputStream inputStream = new BufferedInputStream(codeimg.getBinaryStream()); //把BLOB数据转为二进制流
BufferedOutputStream outputStream = new BufferedOutputStream(ServletActionContext.getResponse().getOutputStream());
byte[] b = new byte[1024];
int len = 0 ;
while ((len=inputStream.read(b))!=-1) {
outputStream.write(b, 0, len);
}
outputStream.flush();
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)