文章目錄
- 一、前言
- 二、pb簡介
- 三、pb簡單使用方法
- 四、例子
- 五、pb工具和模型免費下載地址
一、前言
在我們開發的程序中,可能會遇到這個問題,要展示一個二維碼,二維碼里有很多資料,但是資料太多了,導致二維碼過密,識別費勁,同時還存在安全問題,比如通過草料等軟體決議,就能夠知道二維碼里的資料,這樣也不安全,這個時候就可以試試pb來進行加密壓縮了,
pb工具的百度網盤鏈接在最下方,
二、pb簡介
pb全稱protobuf,proto協議的生成和決議是開源代碼,在github上搜com.google.protobuf就可以了,
三、pb簡單使用方法
-
生成模型;
創建一個proto檔案,格式使用proto3的語法 -
編譯成java或C#物體類
利用protoc工具生成物體類
包內包含了編譯工具protoc.exe、一個proto格式樣例、一個批量轉換器 -
添加protobuf的包進行創建和決議
將生成的物體類檔案直接加入到工程中,并在pom檔案中增加對應的包
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.11.4</version>
</dependency>
然后直接可以使用了,
四、例子
-
在pb工具和模型中定義

這里最好用a,b,c這樣命名,可以減少字符長度, -
點擊bat,會生成一個包,里面包含一個檔案,

把上面生成的檔案放到專案中, -
然后就可以進行呼叫了,下面相當于偽代碼不能直接用,也不是核心,
if (b) {
returnMsgEntity1.setCode(200);
// 資料加密 這里缺少一項總金額
ProxyApplactionDTO.Qrcode builder = ProxyApplactionDTO.Qrcode.newBuilder()
.setA(data.getEnterprise_code())
.setB(data.getEnterprise_name())
.setC(data.getBank() == null ? "" : data.getBank())
.setD(data.getAccount() == null ? "" : data.getAccount())
.setE(data.getAddress() == null ? "" : data.getAddress())
.setF(data.getPhone() == null ? "" : data.getPhone())
.setG(data.getTotal_price())
.setH(data.getNum())
.setI(data.getProduct_name())
.setJ(data.getSeller_phone())
.setK(data.getSeller_address())
.setL(data.getUnit() == null ? "" : data.getUnit())
.setM(data.getModel() == null ? "" : data.getModel())
.setN(data.getNote() == null?"":data.getNote())
.setO(data.getAll_price())
.build();
byte[] compress = new byte[1024];
try {
compress = CompressUtil.compressByte(builder.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
// base64
String base64 = Base64Util.encode(compress);
redisService.setNoSeria("proxy","code",base64,1000*60*60);
// 解
byte[] decode = Base64Util.decode(base64);
byte[] bytes = CompressUtil.uncompressByte(decode);
ProxyApplactionDTO.Qrcode p2 = ProxyApplactionDTO.Qrcode.parseFrom(bytes);
- 核心代碼,
CompressUtil工具類,主要是壓縮解壓縮的,
package com.aisino.util.compress;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class CompressUtil {
// 壓縮
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return out.toString("ISO-8859-1");
}
// 壓縮
public static byte[] compressByte(byte[] bytes) throws IOException {
if (bytes == null || bytes.length == 0) {
return bytes;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(bytes);
gzip.close();
return out.toByteArray();
}
// 解壓縮
public static byte[] uncompressByte(byte[] bytes) throws IOException {
if (bytes == null || bytes.length == 0) {
return bytes;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平臺默認編碼,也可以顯式的指定如toString("GBK")
return out.toByteArray();
}
// 解壓縮
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(str
.getBytes("ISO-8859-1"));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平臺默認編碼,也可以顯式的指定如toString("GBK")
return out.toString();
}
}
通過以上就能夠吧二維碼的資料壓縮,并且資料越多,越好用,
五、pb工具和模型免費下載地址
下載地址
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/389135.html
標籤:java
