我有一个 Dropwizard 应用程序,其中客户端请求正文内容是压缩的内容。我需要在 Dropwizard 应用程序中解压缩内容。我有以下代码,但是我在该行收到了异常 java.io.EOFException
:GZIPInputStream is = new GZIPInputStream(new ByteArrayInputStream(gzipBody))
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.zip.GZIPInputStream;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
@Path("/")
public class UserEventResource {
@POST
@Path("/save")
@Produces("application/json;charset=utf-8")
public Response save(byte[] gzipBody) {
try {
try (GZIPInputStream is = new GZIPInputStream(new ByteArrayInputStream(gzipBody))) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
String body = new String(os.toByteArray(), Charset.forName("UTF-8"));
}
}
return Response.status(OK).build();
} catch (Exception exception) {
return Response.status(INTERNAL_SERVER_ERROR).build();
}
}
}
客户端正在发送以下请求:
curl -XPOST -d @test.gz http://localhost:8080/save
test.gz是按照以下步骤创建的:
echo "hello world" > test
gzip test