从HttpServletRequest

我有一个API,客户端可以通过发送压缩的请求向其发出请求。使用Deflator的以下方式进行压缩。

请求有效载荷:

public byte[] compressRequest(String requestString) {
    int reqLength = requestString.length();
    int temp = (int)(1.001D * (double)reqLength + 1.0D) + 12;
    byte[] byteArr = requestString.getBytes("US-ASCII");
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(temp);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream);
    deflaterOutputStream.write(byteArr);
    deflaterOutputStream.close();
    return byteArrayOutputStream.toByteArray();
}

在服务器端,我试图按照以下方式提取和解压缩数据。

相同的代码在基于Java7和Servlet的应用程序上运行良好。但是在基于Java 8和基于Spring Boot的应用程序中,我在行java.io.EOFException: Unexpected end of ZLIB input stream的第inputLine = in.readLine())行之后出现了异常@PostMapping(value = "saverecord") public void hello(HttpServletRequest request,HttpServletResponse response) { response.setContentType("application/binary"); InflaterInputStream is = InflaterInputStream(request.getInputStream()); String requestXMLStr = extract(is,0); ServletOutputStream out = response .getOutputStream(); // some processing and return response } private String extract(InputStream is) throws IOException { StringBuffer sb = new StringBuffer(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); } return sb.toString(); }

InflaterInputStream is = InflaterInputStream(request.getInputStream(),new Inflater(false)); // tried true also

我尝试遵循其他SO答案中建议的两件事,但均无济于事。

BufferedReader in = new BufferedReader(new InputStreamReader(is,"US-ASCII"));

尝试设置编码

@RequestBody

PS:出于某些项目特定的原因,我无法在项目中使用"eofline": true,

j5899965 回答:从HttpServletRequest

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3161924.html

大家都在问