
本文共 7005 字,大约阅读时间需要 23 分钟。
���GZIP������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������GZIP������������������������������������������������������������������������������������
���������������������������������������������
1. ���������������������������
GZIP���������������������������������������������������������������������������������������������������DEFLATE���������������������������������������������������������������GZIP������������������������������
2. ���������������������
��������������������������������������������������� CompressUtils
������������������������������������������������������������������������������UTF-8���ISO-8859-1���������������������������������������
package com.yolanda.myspringboot.compress;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;public class CompressUtils { private static final String GZIP_ENCODE_UTF_8 = "UTF-8"; private static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1"; private static final int BYTE_NUM = 256; public static byte[] compress(String str) throws IOException { return compressData(str, GZIP_ENCODE_UTF_8); } public static String decompressToStr(String byteStr) { try { byte[] bytes = byteStr.getBytes(GZIP_ENCODE_ISO_8859_1); return decompressDataToStr(bytes, GZIP_ENCODE_UTF_8); } catch (UnsupportedEncodingException e) { System.out.println("������������������������������" + e.getMessage()); } return null; } private static byte[] compressData(String str, String encoding) throws IOException { if (str == null || str.isEmpty()) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); try { gzip.write(str.getBytes(encoding)); gzip.close(); } catch (IOException e) { System.out.println("���������������������" + e.getMessage()); } return out.toByteArray(); } private static String decompressDataToStr(byte[] bytes, String encoding) throws IOException { if (bytes == null || bytes.length == 0) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(bytes); try { GZIPInputStream ungzip = new GZIPInputStream(in); byte[] buffer = new byte[BYTE_NUM]; int n; while ((n = ungzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } return out.toString(encoding); } catch (IOException e) { System.out.println("������������������������" + e.getMessage()); } return null; }}
3. ���������������
���������������������������������������������������������������������������������������������������������������������
package com.yolanda.mycompressclient;import java.io.IOException;public class Main { public static void main(String[] args) { String test = "test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890 test1234567890"; System.out.println("���������������" + test.length()); try { String afterCompressStr = CompressUtils.compress(test); System.out.println("���������������" + afterCompressStr.length()); } catch (IOException e) { System.out.println(e.getMessage()); } }}
���������������������������������������������500������������������������������������������������������������������������
4. ������Spring Boot���������������������������
������Spring Boot���������������������������������������������������������������������������������������������������������������������������������������������
���������������
package com.yolanda.myspringboot;import com.yolanda.myspringboot.compress.CompressUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller@RequestMapping("/hello")@ResponseBodypublic class HelloController { @RequestBody public String hello(@RequestBody String data) { return "Hello, Yolanda! ������������������������������������" + CompressUtils.decompressToStr(data); }}
���������������
package com.yolanda.mycompressclient.controller;import com.yolanda.mycompressclient.http.HttpClient;import com.yolanda.mycompressclient.http.impl.HttpClientImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;@Controllerpublic class TestController { @Autowired HttpClient httpClient; @RequestMapping(value = "/compress") @ResponseBody public ResponseEntityhello() { String_url_ = "http://localhost:8081/hello"; HttpMethod method = HttpMethod.GET; MultiValueMap params = new LinkedMultiValueMap<>(); String testStr = "test1234567890 test1234567890 ... "; // ������������������������ try { byte[] compressedData = CompressUtils.compress(testStr); if (compressedData != null && compressedData.length > 0) { params.add("data", new String(compressedData, "ISO-8859-1")); } else { System.out.println("������������"); } } catch (IOException e) { return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } ResponseEntity response = httpClient.client(url, method, params); return response; }}
5. ������������������������
���������������������������������������������������������������������������������������������������
server.port=8081
������������������������������HTTP������������������������������������������
������
������������������������������������������������������������������������������������������GZIP���������������WindowState���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
