我在一项非常简单的任务中遗漏了一些东西:我必须检查传入的请求输入流是否已压缩并解压缩其内容。这不使用内容类型。我想过这样的事情:
检查请求是否被压缩:
private boolean isGZIPed( HttpServletRequest request )
{
boolean isGziped = false;
try
{
PushbackInputStream pb = new PushbackInputStream( request.getInputStream(), 2 );
byte[] signature = new byte[2];
pb.read( signature );
pb.unread( signature );
int head = ( (int)signature[0] & 0xff ) | ( ( signature[1] << 8 ) & 0xff00 );
isGziped = GZIPInputStream.GZIP_MAGIC == head;
}
catch( Exception ioe )
{
logger.error(ioe);
}
return isGziped;
}
如果它确实是 gziped,请解压缩它:
if(isGziped(request)
{
GZIPInputStream gzis = new GZIPInputStream( request.getInputStream() );
InputStreamReader reader = new InputStreamReader( gzis );
...
}
我的问题是 new GZIPInputStream(request.getInputStream()) 总是抛出“不是 gzip 格式”异常。我想念什么?