记:后端对字符串进行gzip压缩,前端js进行gzip解压

您所在的位置:网站首页 字符串压缩解压工具 记:后端对字符串进行gzip压缩,前端js进行gzip解压

记:后端对字符串进行gzip压缩,前端js进行gzip解压

2024-06-03 05:10| 来源: 网络整理| 查看: 265

public static void main(String[] args) { try { String longString = "www.baidu.com"; // GZIP压缩后的数据 byte[] compress = compress(longString.getBytes()); //通过Base64转成字符串 String longStringEncoded = Base64.getEncoder().encodeToString(compress); } catch (IOException e) { e.printStackTrace(); }}

public static byte[] compress(byte[] data) throws IOException { if (data == null || data.length == 0) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(data); gzip.close(); return out.toByteArray();}

public static byte[] uncompress(byte[] data) throws IOException { if (data == null || data.length == 0) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream inputStream = new ByteArrayInputStream(data); GZIPInputStream gzip = new GZIPInputStream(inputStream); byte[] buffer = new byte[256]; int n; while ((n = gzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } gzip.close(); inputStream.close(); return out.toByteArray();}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3