我有一个使用Spring Boot开发的REST服务,试图将图像数据发送回客户端,然后将其显示在浏览器中。然而,当我尝试,我得到了以下错误:在REST响应中发送图像时请求标头太大
2016-09-26 08:40:31.897 INFO 6435 --- [nio-8080-exec-7] o.a.coyote.http11.Http11NioProcessor : Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Request header is too large
at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:111) ~[tomcat-embed-core-8.0.36.jar!/:8.0.36]
at org.apache.coyote.http11.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:267) ~[tomcat-embed-core-8.0.36.jar!/:8.0.36]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1013) ~[tomcat-embed-core-8.0.36.jar!/:8.0.36]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670) [tomcat-embed-core-8.0.36.jar!/:8.0.36]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) [tomcat-embed-core-8.0.36.jar!/:8.0.36]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) [tomcat-embed-core-8.0.36.jar!/:8.0.36]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_60]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_60]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.36.jar!/:8.0.36]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_60]
我的服务代码如下:
@RequestMapping(method = RequestMethod.POST, value = "/com")
public String handleCompare(@RequestParam("testid") String test,
RedirectAttributes redirectAttributes, Model model) throws IOException {
File f=new File("Sampletest.jpg");
BufferedImage origImg=ImageIO.read(f);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(origImg, "jpg", baos);
byte[] imageInByte=baos.toByteArray();
byte[] encoded= Base64.encodeBase64(imageInByte);
String encodedString = new String(encoded);
model.addAttribute("webcamattr", encodedString);
return "resp";
}
的“resp.html”相关部分如下:
<table>
<tr>
<td align="center"><b>Response Image</b></td>
</tr>
<tr th:each="webfile : ${webcamattr}">
<td> <img width="300" height="300" src="data:image/jpeg;base64" th:src="${webcamattr}"></img></td>
</tr>
</table>
我application.properties文件如下:
multipart.maxFileSize=10Mb
multipart.maxRequestSize=10Mb
我想发送的文件大小只有19KB,但我不确定它为什么会抛出大的请求头错误。你能否帮助确定我做错了什么?图像数据是否在请求标头中传递?如果是这样,我怎么能发送它在响应体?
谢谢
你为什么要给我们展示'resp.html'?在你的''handleCompare()'方法被调用之前,对'http:// server/context/com'的POST请求正在被拒绝。客户端发送错误的数据,因此请检查客户端代码,和/或登录请求标头。 – Andreas
@Andreas请求没有被拒绝。对比较的调用是成功的,因为我已经验证了这一点。响应发送到“resp.html”时遇到此错误 – seriousgeek
真的吗?因为该堆栈跟踪会指示其他情况。 – Andreas