开发一个大文件下载功能时,客户端老是报这样的错:
org.apache.http.MalformedChunkCodingException: Unexpected content at the end of chunk
at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:259) ~[httpcore-4.4.1.jar!/:4.4.1]
at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:227) ~[httpcore-4.4.1.jar!/:4.4.1]
at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:186) ~[httpcore-4.4.1.jar!/:4.4.1]
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137) ~[httpclient-4.4.1.jar!/:4.4.1]
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:150) ~[httpclient-4.4.1.jar!/:4.4.1]
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2146) ~[commons-io-2.5.jar!/:2.5]
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102) ~[commons-io-2.5.jar!/:2.5]```
搜了百度谷歌,遇到这种错误的人不多,没啥解决办法。
用游览器访问下载链接,文件正常下载,没问题(暗坑)------
于是怀疑是代码问题。开始以为是httpcore库的版本问题,换了和服务器一样的版本,还是出错,实在没辙了看了ChunkedInputStream的代码,发现read的逻辑没问题啊。
这么了几个小时,实在没辙了,为什么游览器行,代码不行?于是用游览器又下载了一遍,尼码,游览器也开始报网络问题。。。
时灵时不灵,哪有这样的?于是看了下服务器的日志,之前都是看业务日志文件,忽略了catalina.out ,这猛地一看
Caused by: org.apache.catalina.connector.ClientAbortException: java.net.SocketTimeoutException
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:356)
at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:815)
at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:720)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:391)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:369)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96)
at org.glassfish.jersey.servlet.internal.ResponseWriter$NonCloseableOutputStreamWrapper.write(ResponseWriter.java:298)
at org.glassfish.jersey.message.internal.CommittingOutputStream.write(CommittingOutputStream.java:229)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$UnCloseableOutputStream.write(WriterInterceptorExecutor.java:299)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1793)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1769)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1744)
看来是tcp超时了,调大客户端请求的tcp超时时间
————————————————————————————
又琢磨了2小时,还是没找到超时原因。调大整了客户端请求的TIME_OUT,还是不成功,怀疑是不是服务端设置了相关超时参数,看了下tomcat配置,没发现配置了什么超时参数。
又开始折腾google,发现一个报错堆栈很相似的记录,
https://blog.51cto.com/vekergu/1677437
这个博客上说是ulimit打开文件数太小,好吧!第二天改大点ulimit,再试,游览器成功,postman上请求,postman老是被搞挂,还是有Unexpected content at the end of chunk报错。。。就像这样吧