应用场景
当字符串太长,
需要将字符串值存入数据库时,如果字段长度不够,则会出现插入失败;
或者需要进行Http传输时,由于参数长度过长造成http传输失败等。
字符串压缩与解压方法
方法一:用 Java8中的gzip
/**
* 使用gzip压缩字符串
* @param str 要压缩的字符串
* @return
*/
public static String compress(String str) {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = null;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (gzip != null) {