核心是压缩和解压。
压缩代码:
public byte[] compress(String str) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = null;
GZIPOutputStream gzip = null;
byte[] compress;
try {
out = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes("utf-8"));
gzip.close();
compress = out.toByteArray();
return compress;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
调用代码这模拟将压缩后的byte[]数组作为请求Body给另外的微服务的url
public String getTollJsonByTile(String coordiate) throws Exception {
/* protobuffer对字符串压缩效果一般,所以还是用GZIP进行压缩。
FmbRequstPb.FmbRequstPbInner.Builder request = FmbRequstPb.FmbRequstPbInner.newBuilder();
request.setCoordinate(coordiate);
request.setCarid(123);
byte[] sentByteArray = request.build().toByteArray();
*/
HttpHeaders requestHeaders = new HttpHeaders();
// Accept
//requestHeaders.set("Accept", "text/");
requestHeaders.set("Accept-Charset", "utf-8");
//对字符串进行压缩
byte[] test = compress(coordiate);
// String sst1= restTemplate.postForObject("http://SYNC/sync/user/find/modeid?parm={parm}",null,String.class,"abcde");
String sst2 = restTemplate.postForObject("http://SYNC/sync/adas/pbf/warn", test, String.class);
return sst2;
}
SYNC这个微服务的接收controller
@RequestMapping(value = "/adas/pbf/warn",method = RequestMethod.POST)
public ResponseEntity getBrotocalBuffer(HttpServletRequest rq,@RequestBody byte[] coordinates) throws IOException {
//FmbRequstPb.FmbRequstPbInner req = FmbRequstPb.FmbRequstPbInner.parseFrom(coordinates);
//return ResponseEntity.ok(req.toByteArray());
//解压缩
String ss = uncompress(coordinates);
return ResponseEntity.ok(ss);
}
解压缩代码:
public String uncompress(byte[] str) {
if (str == null || str.length== 0) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
GZIPInputStream gzip = null;
String uncompress = "";
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(str);
gzip = new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = gzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
uncompress = out.toString("utf-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != gzip) {
try {
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return uncompress;
}
分享到:
相关推荐
springmvc+spring线程池处理http并发请求数据同步控制问题
主要介绍了SpringBoot+SpringSecurity处理Ajax登录请求问题,本文给大家介绍的非常不错,具有参考借鉴价值,需要的朋友可以参考下
spring源码文件压缩
Spring Cloud Gateway的全局异常处理 Spring Cloud Gateway中的全局异常处理不能直接用@ControllerAdvice来处理,通过跟踪异常信息的抛出,找到对应的源码,自定义一些处理逻辑来符合业务的需求。 网关都是给接口做...
spring的源码文件压缩
Spring API 英文版 自己压缩CSS干净CHM格式 视觉舒适
NULL 博文链接:https://13shu.iteye.com/blog/2021652
主要给大家介绍了关于Spring MVC请求参数与响应结果全局加密和解密的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在接入Spring-Cloud-Gateway时,可能有需求进行缓存Json-Body数据或者Form-Urlencoded数据的情况。这篇文章主要介绍了SpringCloud Finchley Gateway 缓存请求Body和Form表单的实现,感兴趣的小伙伴们可以参考一下
基于Spring Batch的大数据量并行处理 基于Spring Batch的大数据量并行处理
该资源提供4中url请求方式,各有不同优缺点,可自行选择,参数可格式话,可拼接
主要介绍了详解SpringCloud Finchley Gateway 统一异常处理,非常具有实用价值,需要的朋友可以参考下
spring 框架 当前最新版本 5.1.8.RELEASE 也包括spring data spring security spring integration
6.Spring RestTemplate中几种常见的请求方式 7.RestTemplate的逆袭之路,从发送请求到负载均衡 8.Spring Cloud中负载均衡器概览 9.Spring Cloud中的负载均衡策略 10.Spring Cloud中的断路器Hystrix 11.Spring ...
spring mvc 拦截器获取请求数据信息 解压之后放到项目中 直接运行就可以了 (将流多次运用)
这段Spring Boot代码展示了如何实现文件的批量下载与压缩。主要功能包括通过HTTP响应将多个文件打包成zip压缩包,并提供给用户一次性下载。以下是代码的主要功能点: 1.文件模拟数据:使用Map模拟了一组文件,其中...
本demo为Spring boot整合shiro,以mybatis plus做dao层交互数据,实现了读取数据库用户数据实现用户登录,权限认证,读取数据库中用户对应的url请求,实现请求的过滤。自定义了relam和过滤器来实现这些功能
spring-batchr的分区示例的简单实现。
NULL 博文链接:https://ajita.iteye.com/blog/1396264
本工程用于研究如何在Spring框架中配置Redis 本工程开发工具:Eclipse 本工程编码方式:UTF-8