BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #53419同步于 2016/10/7
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

遇到java的一个IO问题 求教

Badluck
2016/10/7镜像同步2 回复
public String request(String url) { long s = System.currentTimeMillis(); StringBuffer sb = new StringBuffer(); InputStreamReader fis = null; try { fis = new InputStreamReader(new URL(url).openStream(), "utf-8"); int c = 0; // 适合信息量比较大的文件 char[] b = new char[1024]; while ((c = fis.read(b)) != -1) { sb.append(new String(b, 0, c)); } LOG.info("[request] url:{}, time:{}", url, (System.currentTimeMillis() - s)); return sb.toString(); } catch (MalformedURLException e) { LOG.info("[request] url:{}, time:{}, res.length:{}, e", url, (System.currentTimeMillis() - s), sb.length(), e); return null; } catch (IOException e) { LOG.info("[request] url:{}, time:{}, res.length:{}, e", url, (System.currentTimeMillis() - s), sb.length(), e); return null; } catch (Exception e) { LOG.info("[request] url:{}, time:{}, res.length:{}, e", url, (System.currentTimeMillis() - s), sb.length(), e); return null; } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { LOG.info("[request] fis.close() , e", url, (System.currentTimeMillis() - s), sb.length(), e); } } } } while ((c = fis.read(b)) != -1) 这段代码 抛了这个异常 IOException:Premature EOF 求问 神马原因呢?
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
nuanyangyang机器人#1 · 2016/10/8
http://stackoverflow.com/questions/13210108/reading-a-web-page-in-java-ioexception-premature-eof 根本原因是,这个异常是HTTPInputStream抛的。 另外,试试Java 1.7的try-with-resource语句吧,比你这样写好看多了。
Badluck机器人#2 · 2016/10/8
谢谢 【 在 nuanyangyang 的大作中提到: 】 : http://stackoverflow.com/questions/13210108/reading-a-web-page-in-java-ioexception-premature-eof 根本原因是,这个异常是HTTPInputStream抛的。 : 另外,试试Java 1.7的try-with-resource语句吧,比你这样写好看多了。