我正在尝试在angular应用程序中使用(pako.js)压缩一个帖子的payload,并通过rest通信在Java后端应用程序中获取答案。
在后端,我编写了一个拦截器,并尝试通过GZIPInputStream解压请求。但是,我总是得到一个“不是GZIP格式”的消息。
问题可能在输入流的编码中,但我无法解决我的问题。我测试了许多解决方案,但没有一个有效。
如果我查看输入流的byte[],这是前几个索引
拦截器代码如下:
问题可能在输入流的编码中,但我无法解决我的问题。我测试了许多解决方案,但没有一个有效。
如果我查看输入流的byte[],这是前几个索引
[31, -62, -117, 8, 0, 0, 0, 0...
我做错了什么?
与Angular有关的部分
var stingtogzip = encodeURIComponent(JSON.stringify(criteria));
var gzipstring= pako.gzip(stingtogzip , { to : 'string'});
options.headers = new Headers();
options.headers.append("Content-Encoding","gzip");
options.body = gzipstring;
options.method = 'POST';
return this.http.request(req, options)
拦截器代码如下:
@Provider
public class GZIPReaderInterceptor implements ReaderInterceptor {
public Object aroundReadFrom(ReaderInterceptorContext ctx)
throws IOException {
String encoding = ctx.getHeaders().getFirst("Content-Encoding");
if (!"gzip".equalsIgnoreCase(encoding)) {
return ctx.proceed();
}
InputStream gzipInputStream = new GZIPInputStream(ctx.getInputStream());
ctx.setInputStream(gzipInputStream);
return ctx.proceed();
}
}