• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java NegativeArraySizeException异常解决方案

java 搞代码 4年前 (2022-01-05) 141次浏览 已收录 0个评论

这篇文章主要介绍了Java NegativeArraySizeException异常解决方案,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

问题描述:服务器接收后台返回的报文时,提示java.lang.NegativeArraySizeException

分析:这种异常返回的原因,一般情况下没有报文提示为返回空报文,初步分析是响应报文流长度出了问题

百度一下类似的情况:https://stackoverflow.com/questions/11207897/negative-array-size-exception

节选部分内容:

 try{ connection = (HttpConnection)Connector.open("http://someurl.xml",Connector.READ_WRITE); 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<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>) 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异常内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是Java NegativeArraySizeException异常解决方案的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java NegativeArraySizeException异常解决方案

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址