java gzip压缩/ungzip解压缩、base64 encode编码/decode编码、URLEncode.encode编码/URLDecoder.decode编码

日萌社

人工智能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 = new char[64];

    static {
        int i = 0;
        for (char c = 'A'; c <= 'Z'; c++)
            map1[i++] = c;
        for (char c = 'a'; c <= 'z'; c++)
            map1[i++] = c;
        for (char c = '0'; c <= '9'; c++)
            map1[i++] = c;
        map1[i++] = '+';
        map1[i++] = '/';
    }

    // Mapping table from Base64 characters to 6-bit nibbles.
    private static byte[] map2 = new byte[128];

    static {
        for (int i = 0; i < map2.length; i++)
            map2[i] = -1;
        for (int i = 0; i < 64; i++)
            map2[map1[i]] = (byte) i;
    }

    /**
     * Encodes a string into Base64 format.
     * No blanks or line breaks are inserted.
     *
     * @param s a String to be encoded.
     * @return A String with the Base64 encoded data.
     */
    public static String encodeString(String s) {
        return new String(encode(s.getBytes()));
    }

    /**
     * Encodes a byte array into Base64 format.
     * No blanks or line breaks are inserted.
     *
     * @param in an array containing the data bytes to be encoded.
     * @return A character array with the Base64 encoded data.
     */
    public static char[] encode(byte[] in) {
        return encode(in, in.length);
    }

    /**
     * Encodes a byte array into Base64 format.
     * No blanks or line breaks are inserted.
     *
     * @param in   an array containing the data bytes to be encoded.
     * @param iLen number of bytes to process in <code>in</code>.
     * @return A character array with the Base64 encoded data.
     */
    public static char[] encode(byte[] in, int iLen) {
        int oDataLen = (iLen * 4 + 2) / 3;       // output length without padding
        int oLen = ((iLen + 2) / 3) * 4;         // output length including padding
        char[] out = new char[oLen];
        int ip = 0;
        int op = 0;
        while (ip < iLen) {
            int i0 = in[ip++] & 0xff;
            int i1 = ip < iLen ? in[ip++] & 0xff : 0;
            int i2 = ip < iLen ? in[ip++] & 0xff : 0;
            int o0 = i0 >>> 2;
            int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
            int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
            int o3 = i2 & 0x3F;
            out[op++] = map1[o0];
            out[op++] = map1[o1];
            out[op] = op < oDataLen ? map1[o2] : '=';
            op++;
            out[op] = op < oDataLen ? map1[o3] : '=';
            op++;
        }
        return out;
    }

    /**
     * Decodes a string from Base64 format.
     *
     * @param s a Base64 String to be decoded.
     * @return A String containing the decoded data.
     * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
     */
    public static String decodeString(String s) {
        return new String(decode(s));
    }

    /**
     * Decodes a byte array from Base64 format.
     *
     * @param s a Base64 String to be decoded.
     * @return An array containing the decoded data bytes.
     * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
     */
    public static byte[] decode(String s) throws IllegalArgumentException
    {
        return decode(s.toCharArray());
    }

    /**
     * Decodes a byte array from Base64 format.
     * No blanks or line breaks are allowed within the Base64 encoded data.
     *
     * @param in a character array containing the Base64 encoded data.
     * @return An array containing the decoded data bytes.
     * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
     */
    public static byte[] decode(char[] in) {
        int iLen = in.length;
        if (iLen % 4 != 0)
            throw new IllegalArgumentException("Length of Base64 encoded input string is not a multiple of 4。base64编码输入字符串的长度不是4的倍数");
        while (iLen > 0 && in[iLen - 1] == '=')
            iLen--;
        int oLen = (iLen * 3) / 4;
        byte[] out = new byte[oLen];
        int ip = 0;
        int op = 0;
        while (ip < iLen) {
            int i0 = in[ip++];
            int i1 = in[ip++];
            int i2 = ip < iLen ? in[ip++] : 'A';
            int i3 = ip < iLen ? in[ip++] : 'A';
            if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
                throw new IllegalArgumentException("Illegal character in Base64 encoded data.");
            int b0 = map2[i0];
            int b1 = map2[i1];
            int b2 = map2[i2];
            int b3 = map2[i3];
            if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
                throw new IllegalArgumentException("Illegal character in Base64 encoded data.");
            int o0 = (b0 << 2) | (b1 >>> 4);
            int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2);
            int o2 = ((b2 & 3) << 6) | b3;
            out[op++] = (byte) o0;
            if (op < oLen)
                out[op++] = (byte) o1;
            if (op < oLen)
                out[op++] = (byte) o2;
        }
        return out;
    }





} // end class Base64Coder
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 = "UTF-8";
    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";


    public static byte[] compress(String str, String encoding)
//    public static byte[] compress(String str, String encoding)
    {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream(str.getBytes().length);
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);

            gzip.write(str.getBytes(encoding));
//            byte[] bytes = out.toByteArray();

            gzip.close();
        } catch ( Exception e) {
            e.printStackTrace();
        }

        byte[] bytes = 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 = 4096;
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in,bufferSize);
            byte[] buffer = new byte[bufferSize];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            ungzip.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        byte[] bytes1 = 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 = 4096;
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        GZIPInputStream ungzip = null;

        ungzip = new GZIPInputStream(in,bufferSize);
        byte[] buffer = new byte[bufferSize];
        int n;
        while ((n = ungzip.read(buffer)) > 0)
        {
            out.write(buffer, 0, n);
            out.flush();
        }

        if (ungzip!=null)
        {
            ungzip.close();
        }
        in.close();
        String s = 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 = "hello world";
//        System.out.println("字符串长度:"+s.length());
//        System.out.println("压缩后::"+compress(s));
//        System.out.println("解压后:"+uncompress(compress(s)));
//        System.out.println("解压字符串后:"+uncompressToString(compress(s)));
//        System.out.println("解压字符串后的字符串长度:"+uncompressToString(compress(s)).length());
//
//
//        String tmp = "H4sIAAAAAAAAAI2Tv47UMBDGXwWZK5fI/xLb221FRYWEkBCKbM+YWLebhMSb1ep0BdUVFIgKHoGWhzqJewuc3J5YEIdIE83P+Wa+8UzeXJE6DdZf1hHI+hkrKWXMyEquSIo7JGtWlpWhUkrKK5rhsc+QLBKyIhDHFFufFjUx3nFhgdMscT4HhjtDQQQqmWCcM4FOaA/KciiNsR5LLx1w44wCqgx41FKL0nihUILiPASqBNcqGD8nARRSW3BaOMXLoLiVQA1jFE3lwYCujDAWjFBQBaOsE9l7cCJU3gSRy6BgUlShcpUMNmjUIZvwnjGlfWVyQ9voyPqKXCxvsmlh6HJrqwXUEw5j7Np8IApalDO2fX+GWcEfvt1harr5UnwH+AABk43bBe4KZ1PhG5u2ccL3hfUpTjEdixc2tptT8HR5yPWK4IRtysKLTd+/inh46QfENufth67HIUUc/+67G8/86YIVdKaAU/R4PzXQxgHKUJZUehvYP7rd5V5m+1OcuievOdvcV/i94rhYqw8RUpP3h2r6CzYY3zW5D84XurPtPk8g7QccTmkfu9SZtXbeSHJ38/n2+8cfXz7dfru5+/phVhxii";
//        byte[] bytes = tmp.getBytes("ISO8859-1");
//        String s1 = new String(bytes, "UTF-8");
//        String urldecode = URLDecoder.decode(s1, "UTF-8");
//        byte[] decode = Base64Coder.decode(urldecode);
//        String s2 = GZIPUtils.uncompressToString(decode);
//        System.out.println(s2);
//    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

あずにゃん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值