[英]pako on client side with java on server side
I'm trying to compress a Post's payload with ( pako .js) in an angular application and get the answer in a Java back-end application via rest communication. 我正在尝试在角度应用程序中使用( pako .js)压缩Post的有效负载,并通过休息通信在Java后端应用程序中获得答案。 In the back-end, I write an interceptor, and try to decompress the request via GZIPInputStream.
在后端,我编写了一个拦截器,并尝试通过GZIPInputStream解压缩请求。 But,I always have a "Not in GZIP format" message.
但是,我总是有一个“不是GZIP格式”的消息。
The problem is probably in the encoding of the inputstream, but I can't figure out how to solve my issue. 问题可能在于输入流的编码,但我无法弄清楚如何解决我的问题。 I test many solutions, but none of them work.
我测试了很多解决方案,但都没有。
If I look at the byte[] of the input stream,this is the first indexes 如果我查看输入流的byte [],这是第一个索引
[31, -62, -117, 8, 0, 0, 0, 0...
What am I doing wrong? 我究竟做错了什么?
Angular's part 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)
The interceptor code: 拦截器代码:
@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();
}
}
finally find the solution : 终于找到了解决方案:
-No need for encodeURIComponent - 不需要encodeURIComponent
-Uses Blob to transport the data to the server - 使用Blob将数据传输到服务器
var stingtogzip = JSON.stringify(criteria);
var gzipstring= pako.gzip(stingtogzip);
var blob = new Blob([gzipString]);
options.headers = new Headers();
options.headers.append("Content-Encoding","gzip");
options.body = blob;
options.method = 'POST';
return this.http.request(req, options)
In my case was necessary the Content-Type header with charset=x-user-defined-binary : 在我的情况下,必须使用charset = x-user-defined-binary的Content-Type标头:
const gzip = pako.gzip(jsonString);
const blob = new Blob([gzip]);
const headers = new Headers({
'Content-Type': 'application/json; charset=x-user-defined-binary',
'Content-Encoding': 'gzip'
});
const reqOptions = new RequestOptions({ headers: headers });
return this.http.put('URL', blob, reqOptions)
.map(this.extractJSON)
.catch((err) => this.httpErrorHandler.handleError(err));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.