Connect getContentLength = -1问题的解决方法
2007-04-06 15:46
531 查看
[align=right][/align]
Connector getContentLength = -1问题的解决方法
以下代码是读取通过http读取url内容
hc = (HttpConnection)Connector.open(url);
in = hc.openInputStream();
int contentLength = (int)hc.getLength();
byte[] raw = new byte[contentLength];
int length = in.read(raw);
in.close();
hc.close();
但当url的内容很少的时候,contentLength 总是返回-1 导致程序出错。
解决的办法是改进读取的方式
hc = (HttpConnection)Connector.open(url);
InputStream in = hc.openInputStream();
System.out.println("hc.getLength()="+hc.getLength()); ;
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[512];
int rc = 0;
while ( (rc = in.read(buff, 0, 512)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] b = swapStream.toByteArray();
in.close();
hc.close();
这样就可以正确读出内容了。
http://www.ooohooo.com/viewthread.php?tid=64&extra=page%3D1
Connector getContentLength = -1问题的解决方法
以下代码是读取通过http读取url内容
hc = (HttpConnection)Connector.open(url);
in = hc.openInputStream();
int contentLength = (int)hc.getLength();
byte[] raw = new byte[contentLength];
int length = in.read(raw);
in.close();
hc.close();
但当url的内容很少的时候,contentLength 总是返回-1 导致程序出错。
解决的办法是改进读取的方式
hc = (HttpConnection)Connector.open(url);
InputStream in = hc.openInputStream();
System.out.println("hc.getLength()="+hc.getLength()); ;
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[512];
int rc = 0;
while ( (rc = in.read(buff, 0, 512)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] b = swapStream.toByteArray();
in.close();
hc.close();
这样就可以正确读出内容了。
http://www.ooohooo.com/viewthread.php?tid=64&extra=page%3D1
相关文章推荐
- 遇到问题---java获取网络文件大小失败getContentLength()为-1 完整解决方法
- 解决 HttpURLConnection类中getContentLength()方法返回-1问题
- 遇到问题---java获取网络文件大小失败getContentLength()为-1 完整解决方法
- getContentLength()= -1 解决方法
- getContentLength()为-1 解决方法
- getContentLength()为-1 解决方法
- getContentLength()为-1 解决方法
- HttpURLConnection.getContentLength()为-1 解决方法
- Android getContentLength()为-1 解决方法
- WebDriver封装GET方法来解决页面跳转不稳定的问题
- file_get_contents() 函数丢包问题解决方法
- mysql连接linux系统提示“登录出现1130 is not allowed to connect to this MySql server”问题,解决方法
- HibernateDaoSupport的getHibernateTemplate.find()方法带参查询返回结果是nul问题解决
- ubantu下解决 Could not get lock /var/lib/dpkg/lock 问题方法
- 解决get方法传递URL参数中文乱码问题
- mysql连接linux系统提示“登录出现1130 is not allowed to connect to this MySql server”问题,解决方法
- [Errno 11004] getaddrinfo failed'问题解决方法
- 解决get方法传递URL参数中文乱码和解决tomcat下中文乱码问题
- rattle安装gtk_buildable_get_name问题解决方法
- ie下$.getJSON出现问题的解决方法