Java字符串的壓縮與解壓縮的兩種方法
應(yīng)用場(chǎng)景
當(dāng)字符串太長(zhǎng),
需要將字符串值存入數(shù)據(jù)庫(kù)時(shí),如果字段長(zhǎng)度不夠,則會(huì)出現(xiàn)插入失??;
或者需要進(jìn)行Http傳輸時(shí),由于參數(shù)長(zhǎng)度過(guò)長(zhǎng)造成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) { try { gzip.close(); } catch (IOException e) { e.printStackTrace(); } } } return new sun.misc.BASE64Encoder().encode(out.toByteArray()); } /** * 使用gzip解壓縮 * @param compressedStr 壓縮字符串 * @return */ public static String uncompress(String compressedStr) { if (compressedStr == null) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = null; GZIPInputStream ginzip = null; byte[] compressed = null; String decompressed = null; try { compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr); in = new ByteArrayInputStream(compressed); ginzip = new GZIPInputStream(in); byte[] buffer = new byte[1024]; int offset = -1; while ((offset = ginzip.read(buffer)) != -1) { out.write(buffer, 0, offset); } decompressed = out.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (ginzip != null) { try { ginzip.close(); } catch (IOException e) { } } if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } return decompressed; }
方法二:用org.apache.commons.codec.binary.Base64
/** * 使用org.apache.commons.codec.binary.Base64壓縮字符串 * @param str 要壓縮的字符串 * @return */ public static String compress(String str) { if (str == null || str.length() == 0) { return str; } return Base64.encodeBase64String(str.getBytes()); } /** * 使用org.apache.commons.codec.binary.Base64解壓縮 * @param compressedStr 壓縮字符串 * @return */ public static String uncompress(String compressedStr) { if (compressedStr == null) { return null; } return Base64.decodeBase64(compressedStr); }
注意事項(xiàng)
在web項(xiàng)目中,服務(wù)器端將加密后的字符串返回給前端,前端再通過(guò)ajax請(qǐng)求將加密字符串發(fā)送給服務(wù)器端處理的時(shí)候,在http傳輸過(guò)程中會(huì)改變加密字符串的內(nèi)容,導(dǎo)致服務(wù)器解壓壓縮字符串發(fā)生異常:
java.util.zip.ZipException: Not in GZIP format
解決方法:
在字符串壓縮之后,將壓縮后的字符串BASE64加密,在使用的時(shí)候先BASE64解密再解壓即可。
到此這篇關(guān)于Java字符串的壓縮與解壓縮的兩種方法的文章就介紹到這了,更多相關(guān)Java字符串壓縮內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布游戲
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02解決mybatisplus MetaObjectHandler 失效的問(wèn)題
本文主要介紹了解決mybatisplus MetaObjectHandler 失效的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Java Chassis3過(guò)載狀態(tài)下的快速失敗解決分析
本文解密了Java Chassis 3快速失敗相關(guān)的機(jī)制和背后故事,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01從零搭建SpringBoot+MyBatisPlus快速開(kāi)發(fā)腳手架
這篇文章主要為大家介紹了從零搭建SpringBoot+MyBatisPlus快速開(kāi)發(fā)腳手架示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Spring中利用IOC實(shí)現(xiàn)注入的方式
Spring IOC(控制反轉(zhuǎn))實(shí)現(xiàn)依賴注入,將對(duì)象創(chuàng)建和依賴關(guān)系的管理交由Spring容器處理,通過(guò)注解或XML配置,實(shí)現(xiàn)對(duì)象之間的松耦合,提高代碼復(fù)用性和可維護(hù)性2023-04-04