日萌社 人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)
1.对发送的数据进行 gzip压缩 、Base64.encode编码、URLEncoder.encode编码,最后再进行http传输数据源数据 --> gzip压缩 --> Base64.encode编码 --> URLEncoder.encode编码 --> http传输数据
2.对http接收到的数据进行 URLDecoder.decode解码、Base64.decode解码、ungzip解压缩 得到最终的原始数据http传输的数据 --> URLDecoder.decode解码 --> Base64.decode解码 --> ungzip解压缩
java 压缩、解压缩时报错 Unexpected end of ZLIB input stream
代码文件下载链接: https://pan.baidu.com/s/19GIva6_AKlFdbVDxbYqLEw
提取码: m6xj
解压缩例子(调用代码):
while(true){String urldecode = null;try{urldecode = URLDecoder.decode(http接收到的数据, "UTF-8");}catch (Exception e){logger.error("URLDecoder.decode方法报错:"+e.getMessage());logger.error("出错的数据:"+split3[1]);continue;}byte[] decode = null;try{decode = Base64Coder.decode(urldecode);}catch (IllegalArgumentException e){logger.error("Base64Coder.decode方法报错:"+e.getMessage());logger.error("出错的数据:"+split3[1]);continue;}String s = null;try{s = GZIPUtils.uncompressToString(decode);}catch (IOException e){logger.error("GZIPUtils.uncompressToString解压数据时出问题:"+e.getMessage());logger.error("出错的数据:"+split3[1]);continue;}}
URLEncoder.encode编码 和 URLDecoder.decode解码 使用java自身提供的jar包类
import java.net.URLDecoder;
import java.net.URLEncoder;
Base64.encode编码 和 Base64.decode解码
package com.nginxlog;public class Base64Coder
{// Mapping table from 6-bit nibbles to Base64 characters.private static char[] map1 &#61; new char[64];static {int i &#61; 0;for (char c &#61; &#39;A&#39;; c <&#61; &#39;Z&#39;; c&#43;&#43;)map1[i&#43;&#43;] &#61; c;for (char c &#61; &#39;a&#39;; c <&#61; &#39;z&#39;; c&#43;&#43;)map1[i&#43;&#43;] &#61; c;for (char c &#61; &#39;0&#39;; c <&#61; &#39;9&#39;; c&#43;&#43;)map1[i&#43;&#43;] &#61; c;map1[i&#43;&#43;] &#61; &#39;&#43;&#39;;map1[i&#43;&#43;] &#61; &#39;/&#39;;}// Mapping table from Base64 characters to 6-bit nibbles.private static byte[] map2 &#61; new byte[128];static {for (int i &#61; 0; i in
.* &#64;return A character array with the Base64 encoded data.*/public static char[] encode(byte[] in, int iLen) {int oDataLen &#61; (iLen * 4 &#43; 2) / 3; // output length without paddingint oLen &#61; ((iLen &#43; 2) / 3) * 4; // output length including paddingchar[] out &#61; new char[oLen];int ip &#61; 0;int op &#61; 0;while (ip >> 2;int o1 &#61; ((i0 & 3) <<4) | (i1 >>> 4);int o2 &#61; ((i1 & 0xf) <<2) | (i2 >>> 6);int o3 &#61; i2 & 0x3F;out[op&#43;&#43;] &#61; map1[o0];out[op&#43;&#43;] &#61; map1[o1];out[op] &#61; op 0 && in[iLen - 1] &#61;&#61; &#39;&#61;&#39;)iLen--;int oLen &#61; (iLen * 3) / 4;byte[] out &#61; new byte[oLen];int ip &#61; 0;int op &#61; 0;while (ip 127 || i1 > 127 || i2 > 127 || i3 > 127)throw new IllegalArgumentException("Illegal character in Base64 encoded data.");int b0 &#61; map2[i0];int b1 &#61; map2[i1];int b2 &#61; map2[i2];int b3 &#61; map2[i3];if (b0 <0 || b1 <0 || b2 <0 || b3 <0)throw new IllegalArgumentException("Illegal character in Base64 encoded data.");int o0 &#61; (b0 <<2) | (b1 >>> 4);int o1 &#61; ((b1 & 0xf) <<4) | (b2 >>> 2);int o2 &#61; ((b2 & 3) <<6) | b3;out[op&#43;&#43;] &#61; (byte) o0;if (op gzip压缩 和 ungzip解压缩
package com.nginxlog;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;public class GZIPUtils
{public static final String GZIP_ENCODE_UTF_8 &#61; "UTF-8";public static final String GZIP_ENCODE_ISO_8859_1 &#61; "ISO-8859-1";public static byte[] compress(String str, String encoding)
// public static byte[] compress(String str, String encoding){if (str &#61;&#61; null || str.length() &#61;&#61; 0) {return null;}ByteArrayOutputStream out &#61; new ByteArrayOutputStream(str.getBytes().length);GZIPOutputStream gzip;try {gzip &#61; new GZIPOutputStream(out);gzip.write(str.getBytes(encoding));
// byte[] bytes &#61; out.toByteArray();gzip.close();} catch ( Exception e) {e.printStackTrace();}byte[] bytes &#61; out.toByteArray();try{out.close();}catch (IOException e) {e.printStackTrace();}return bytes;
// return new String(Base64Coder.encode(bytes));//Base64Coder 对 压缩数据 进行 编码}public static byte[] compress(String str) throws IOException{return compress(str, GZIP_ENCODE_UTF_8);}public static byte[] uncompress(byte[] bytes){int bufferSize &#61; 4096;if (bytes &#61;&#61; null || bytes.length &#61;&#61; 0) {return null;}ByteArrayOutputStream out &#61; new ByteArrayOutputStream(bytes.length);ByteArrayInputStream in &#61; new ByteArrayInputStream(bytes);try {GZIPInputStream ungzip &#61; new GZIPInputStream(in,bufferSize);byte[] buffer &#61; new byte[bufferSize];int n;while ((n &#61; ungzip.read(buffer)) >&#61; 0) {out.write(buffer, 0, n);}ungzip.close();} catch (Exception e) {e.printStackTrace();}byte[] bytes1 &#61; out.toByteArray();try{in.close();out.close();}catch (IOException e){e.printStackTrace();}return bytes1;}public static String uncompressToString(byte[] bytes, String encoding) throws IOException {int bufferSize &#61; 4096;if (bytes &#61;&#61; null || bytes.length &#61;&#61; 0) {return null;}ByteArrayOutputStream out &#61; new ByteArrayOutputStream(bytes.length);ByteArrayInputStream in &#61; new ByteArrayInputStream(bytes);GZIPInputStream ungzip &#61; null;ungzip &#61; new GZIPInputStream(in,bufferSize);byte[] buffer &#61; new byte[bufferSize];int n;while ((n &#61; ungzip.read(buffer)) > 0){out.write(buffer, 0, n);out.flush();}if (ungzip!&#61;null){ungzip.close();}in.close();String s &#61; out.toString(encoding);out.close();return s;}public static String uncompressToString(byte[] bytes) throws IOException {return uncompressToString(bytes, GZIP_ENCODE_UTF_8);}// public static void main(String[] args) throws IOException
// {
// String s &#61; "hello world";
// System.out.println("字符串长度&#xff1a;"&#43;s.length());
// System.out.println("压缩后&#xff1a;&#xff1a;"&#43;compress(s));
// System.out.println("解压后&#xff1a;"&#43;uncompress(compress(s)));
// System.out.println("解压字符串后&#xff1a;"&#43;uncompressToString(compress(s)));
// System.out.println("解压字符串后的字符串长度&#xff1a;"&#43;uncompressToString(compress(s)).length());
//
//
// String tmp &#61; "H4sIAAAAAAAAAI2Tv47UMBDGXwWZK5fI/xLb221FRYWEkBCKbM&#43;YWLebhMSb1ep0BdUVFIgKHoGWhzqJewuc3J5YEIdIE83P&#43;Wa&#43;8UzeXJE6DdZf1hHI&#43;hkrKWXMyEquSIo7JGtWlpWhUkrKK5rhsc&#43;QLBKyIhDHFFufFjUx3nFhgdMscT4HhjtDQQQqmWCcM4FOaA/KciiNsR5LLx1w44wCqgx41FKL0nihUILiPASqBNcqGD8nARRSW3BaOMXLoLiVQA1jFE3lwYCujDAWjFBQBaOsE9l7cCJU3gSRy6BgUlShcpUMNmjUIZvwnjGlfWVyQ9voyPqKXCxvsmlh6HJrqwXUEw5j7Np8IApalDO2fX&#43;GWcEfvt1harr5UnwH&#43;AABk43bBe4KZ1PhG5u2ccL3hfUpTjEdixc2tptT8HR5yPWK4IRtysKLTd&#43;/inh46QfENufth67HIUUc/&#43;67G8/86YIVdKaAU/R4PzXQxgHKUJZUehvYP7rd5V5m&#43;1OcuievOdvcV/i94rhYqw8RUpP3h2r6CzYY3zW5D84XurPtPk8g7QccTmkfu9SZtXbeSHJ38/n2&#43;8cfXz7dfru5&#43;/phVhxii";
// byte[] bytes &#61; tmp.getBytes("ISO8859-1");
// String s1 &#61; new String(bytes, "UTF-8");
// String urldecode &#61; URLDecoder.decode(s1, "UTF-8");
// byte[] decode &#61; Base64Coder.decode(urldecode);
// String s2 &#61; GZIPUtils.uncompressToString(decode);
// System.out.println(s2);
// }
}