问题描述:服务器接收后台返回的报文时,提示java.lang.NegativeArraySizeException
分析:这种异常返回的原因,一般情况下没有报文提示为返回空报文,初步分析是响应报文流长度出了问题
百度一下类似的情况:https://stackoverflow.com/questions/11207897/negative-array-size-exception
节选部分内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | try { URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false ); postData.append( "username" , "loginapi" ); postData.append( "password" , "myapilogin" ); postData.append( "term" , word); connection.setRequestMethod(HttpConnection.POST); connection.setRequestProperty( "Content-Type" , "application/x-www-form-urlencoded" ); connection.setRequestProperty( "User-Agent" , "Profile/MIDP-2.0 Configuration/CLDC-1.0" ); requestOut = connection.openOutputStream(); requestOut.write(postData.getBytes()); String contentType = connection.getHeaderField( "Content-type" ); detailIn = connection.openInputStream(); int length = ( int ) connection.getLength(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (length > 0 ){ //这里长度没有判定的情况下,byte array长度若为-1会产生错误 byte info[] = new byte [length]; int bytesRead = detailIn.read(info); while (bytesRead > 0 ) { baos.write(info, 0 , bytesRead); bytesRead = detailIn.read(info); } baos.close(); connection.close(); requestSuceeded(baos.toByteArray(), contentType); detailIn.read(info); } else { System.out.println( "Negative array size" ); } requestOut.close(); detailIn.close(); connection.close(); } |
结论:HTTP服务器在返回响应报文的时候,没有进行content.length长度判断,按照常规流程响应了错误长度的报文,导致了接收方报文长度异常
到此这篇关于Java NegativeArraySizeException异常解决方案的文章就介绍到这了,更多相关Java NegativeArraySizeException异常内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/220738/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)