我正在尝试将数据从HttpClient发送到使用嵌入式Jetty的服务器,托管在同一台机器上。以下是我得到的完整堆栈跟踪错误:
java.util.zip.ZipException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:165)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
at wserver.UploadHandler.handle(UploadHandler.java:44)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1162)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1096)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
at org.eclipse.jetty.server.Server.handle(Server.java:518)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:244)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:246)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:156)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
at java.lang.Thread.run(Thread.java:745)
2016-04-05 16:17:47.540:WARN:oejh.HttpParser:qtp1452126962-16: parse exception: java.lang.IllegalStateException: too much data seeking EOF in CLOSE for HttpChannelOverHttp@29d04743{r=1,c=false,a=IDLE,uri=null}
此处发生错误,在实例化GZIPInputStream对象的行上:
ServletInputStream bodyInputStream = request.getInputStream();
GZIPInputStream gis = new GZIPInputStream(bodyInputStream);
其中request
是HttpServletRequest。
这是我将字符串压缩为gzip的方式:
public static byte[] gzip(String content) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
gzos.write(content.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
这就是我发送使用HttpClient的方式:
byte[] result = Utils.gzip(pagesJson);
HttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://" + serverHost + "/upload");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("pages", result, ContentType.DEFAULT_BINARY, "pages.bin");
HttpEntity multipart = builder.build();
httpPost.addHeader(HTTP.CONTENT_ENCODING, "gzip");
httpPost.setEntity(multipart);
HttpResponse response = client.execute(httpPost);
我很确定我搞砸了什么,但我不确定到底在哪里。谢谢你的帮助。
答案 0 :(得分:0)
如果您只想发送gzip文件,则无需设置标题。
httpPost.addHeader(HTTP.CONTENT_ENCODING, "gzip");
在服务器端,您还需要处理多部分请求,为此您可以使用Commons FileUpload