前端GZIP压缩之后,后端使用JAVA解密

1.准备

需要一个jar:

1
2
3
4
5
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>

需要一个工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.things.comm;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

@Log4j2
public class GZipUtil {
/**
* 压缩GZip
*
* @return String
*/
public static String gZip(String input) {
byte[] bytes = null;
GZIPOutputStream gzip = null;
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(bos);
gzip.write(input.getBytes(CharEncoding.UTF_8));
gzip.finish();
gzip.close();
bytes = bos.toByteArray();
bos.close();
} catch (Exception e) {
log.error("压缩出错:", e);
} finally {
try {
if (gzip != null)
gzip.close();
if (bos != null)
bos.close();
} catch (final IOException ioe) {
log.error("压缩出错:", ioe);
}
}
return Base64.encodeBase64String(bytes);
}

/**
* 解压GZip
*
* @return String
*/
public static String unGZip(String input) {
byte[] bytes;
String out = input;
GZIPInputStream gzip = null;
ByteArrayInputStream bis;
ByteArrayOutputStream bos = null;
try {
bis = new ByteArrayInputStream(Base64.decodeBase64(input));
gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num;
bos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, num);
}
bytes = bos.toByteArray();
out = new String(bytes, CharEncoding.UTF_8);
gzip.close();
bis.close();
bos.flush();
bos.close();
} catch (Exception e) {
log.error("解压出错:", e);
} finally {
try {
if (gzip != null)
gzip.close();
if (bos != null)
bos.close();
} catch (final IOException ioe) {
log.error("解压出错:", ioe);
}
}
return out;
}
}

2.实现

实现非常简单,直接贴代码。

仅仅转换到json字符串,后面转换对象或者别的怎么用就随便搞了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.things.comm;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import static com.things.comm.GZipUtil.unGZip;

/**
* @Description:
* @ClassName:van
* @Author:Player
* @Date:2019-06-13 14:03
**/
public class van {

public static void main(String[] args) throws UnsupportedEncodingException {
String json="H4sIAAAAAAAAA+1cWXPbOBL+Lfugmt2HuHhTfJTkOEmVc1Ti3ezbFESCEtYQqSKh2MqvH/AAiIYk8zDjkbKOKioTR7MvfN0NkJr484ll/cBZTtKE/zWxZ/zbNK+MK6O4tBb8O19jzPK6t5wweetO5v5k5k7eOpP59SRYTN5OJzNzMpua6rgEbbCk2japvFeEY7Sj8G5hSt9jFOHsa/rwHpPVmlW9llHNyYrmon+R0u8kYuuq21G7lVnmlW1XPSEYbxrGxL+WcxbpLhFTLDl+t0mUDrPuQCEjPzBnT1BSmzlXNZmqla2x1Mqhlj7HMQmx1Ec5mFNIs64zlii8X2WcxwiYAtU8uG4toXa90q6XzbVQSkPZ6kLZNiFl24CU7UbdDD+yLtwagCKkB6l14dCbAnJTB9AzDU9SRGGIk04cBlBoU1enaWtEu6nSh1QtSNUJNKJ2B6KmBy0vr5fNNaTqDPAnM7BOWKmi6fa3k2lqhgp0Q3ldxBdrV1D1bUDVNyXR9X6LM0qS+w5kofRBoHHaWD9OKU0fcPS+B3HTCQB5XyPPFcPJS7Y5JpJkdZMKtOLf1zhZ/ZcgDvXGrYDEikQa7U+MtCTFCDGksllc36ElBWhmnrz4gehOCQfWzefZNWL4n47tBP6/+LXkJmd72oz888/ljlD2IflWNFsqR9YJ+mY7HbOmc5RUR75dyx2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