Java数据压缩HTTP方式传输Gzip(附带测试代码及springboot-HTTP客户端服务端代码)
发布日期:2021-05-10 11:42:32 浏览次数:19 分类:精选文章

本文共 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")
@ResponseBody
public 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;
@Controller
public class TestController {
@Autowired
HttpClient httpClient;
@RequestMapping(value = "/compress")
@ResponseBody
public ResponseEntity
hello() {
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���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:JAVA数据加密压缩传输给服务端(Gzip加AES)
下一篇:VUEJS项目实践六之No PostCSS Config found in

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月14日 02时32分56秒