100分请教一个BufferedReader的问题 try to use StringBuffer pageContext instead of String. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你没有关闭连接. 加代码 finally { if(stdIn !=null) try { stdIn .close(); } catch(IOException e) {} } 呵呵,连接关闭了的,整个代码是: try { String strHtml = ""; while((strHtml = stdIn.readLine())!=null) { pageContent = pageContent + strHtml; } } catch (Exception e) { //log.error(e.toString()); return pageContent; } finally { try { if(stdIn != null) stdIn.close(); } catch (Exception e) { //System.out.println("\n\rBufferedReader:" + e.getMessage()); log.debug(e.toString()); } } } 贴出整个方法吧:public static String getPageContent(String url){ BatchLog log = new BatchLog(); URL stdURL = null; BufferedReader stdIn = null; HttpURLConnection httpCon = null; String pageContent = ""; int responseCode = 0; try { stdURL = new URL(url); } catch (MalformedURLException e) { log.error( "网络链结错误"+e.toString()); } try{ httpCon = (HttpURLConnection)stdURL.openConnection(); } catch(Exception e){ log.error(e.toString()); } try{ responseCode = httpCon.getResponseCode(); } catch(Exception e){ log.error(e.toString()); } if(responseCode == 200){ try { stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream())); } catch (Exception e) { log.error(e.toString()); } /***把URL指定的页面以流的形式读出***/ try { String strHtml = ""; while((strHtml = stdIn.readLine())!=null) { pageContent = pageContent + strHtml; } } catch (Exception e) { //log.error(e.toString()); return pageContent; } finally { try { if(stdIn != null) stdIn.close(); } catch (Exception e) { //System.out.println("\n\rBufferedReader:" + e.getMessage()); log.debug(e.toString()); } } } return pageContent; } String strHtml = "";StringBuffer sb = new StringBuffer(pageContent);while((strHtml = stdIn.readLine())!=null) { sb.append(strHtml).append("\r\n");}pageContent = sb.toString();但是如果最后pageContent太大了,很容易会出现OutOfMemory的。小心 呵呵,关于改成StringBuffer各位能说说原因吗?谢谢了 你用string + 的话每次加上一个字符串都会生成一个新的对象。耗费空间。 我试了一下,没有出现你说的问题,叶面内容很快出来了,我试了一下sina首页,大概5秒钟,改为StringBuffer,大概2秒 e.getMessage()这个方法最好在实际开发中不要使用,很多时候,这个方法不会返回任何东西, 如果你的是一个HTTP请求后返回的流的确可能存在一个问题:HTTP并不是每次请求完成以后都必须关闭这个连接,而只是在HTTP头上面有一个Content-Length: 12345 来声明返回结果的大小,结果在后面的数据段中,它会返回12345个字节然后就停止不动了,因为这个时候其实你是可以再通过这个连接来发送下一次请求,所以readLine就总是不能返回!这个机制是为了提高效率而做的,如果你需要避免这种情况,在发送请求HTTP头的时候就必须增加以下一行:Connection: close这样对方就会在发送完成数据以后自动关闭这个连接,这样readLine就会退出了。以上问题只是一种可能性,具体原因你还需要再查明 请问加一行Connection: close具体应该怎么操作? 求教下为啥不能分页 关于参数传递的问题 hibernate入门问题,这个使用hibernate写入oracle数据库的例子错在哪儿? java怎样向oracle存取图片 求助?在线等 请教java打印程序 请教一下spring如何配置的问题.谢谢大家帮助. 初学struts的问题 JDBC 通过软件或者硬件阻止Android系统被root或者刷机 这个SocketException大概是什么引起的呢? [STRUTS] 如何在ACTION中弹出一个文件选择对话框?
finally {
if(stdIn !=null) try { stdIn .close(); } catch(IOException e) {}
}
try {
String strHtml = "";
while((strHtml = stdIn.readLine())!=null) {
pageContent = pageContent + strHtml;
}
} catch (Exception e) {
//log.error(e.toString());
return pageContent;
} finally {
try {
if(stdIn != null)
stdIn.close();
}
catch (Exception e) {
//System.out.println("\n\rBufferedReader:" + e.getMessage());
log.debug(e.toString());
}
}
}
public static String getPageContent(String url){
BatchLog log = new BatchLog();
URL stdURL = null;
BufferedReader stdIn = null;
HttpURLConnection httpCon = null;
String pageContent = "";
int responseCode = 0; try {
stdURL = new URL(url);
}
catch (MalformedURLException e) {
log.error( "网络链结错误"+e.toString());
} try{
httpCon = (HttpURLConnection)stdURL.openConnection();
}
catch(Exception e){
log.error(e.toString());
} try{
responseCode = httpCon.getResponseCode();
}
catch(Exception e){
log.error(e.toString());
} if(responseCode == 200){
try {
stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream()));
}
catch (Exception e) {
log.error(e.toString());
} /***把URL指定的页面以流的形式读出***/
try {
String strHtml = "";
while((strHtml = stdIn.readLine())!=null) {
pageContent = pageContent + strHtml;
}
} catch (Exception e) {
//log.error(e.toString());
return pageContent;
} finally {
try {
if(stdIn != null)
stdIn.close();
}
catch (Exception e) {
//System.out.println("\n\rBufferedReader:" + e.getMessage());
log.debug(e.toString());
}
}
}
return pageContent;
}
StringBuffer sb = new StringBuffer(pageContent);
while((strHtml = stdIn.readLine())!=null) {
sb.append(strHtml).append("\r\n");
}pageContent = sb.toString();但是如果最后pageContent太大了,很容易会出现OutOfMemory的。小心
Content-Length: 12345 来声明返回结果的大小,
结果在后面的数据段中,它会返回12345个字节然后就停止不动了,因为这个时候其实你是可以再通过这个连接来发送下一次请求,所以readLine就总是不能返回!这个机制是为了提高效率而做的,如果你需要避免这种情况,在发送请求HTTP头的时候就必须增加以下一行:
Connection: close这样对方就会在发送完成数据以后自动关闭这个连接,这样readLine就会退出了。
以上问题只是一种可能性,具体原因你还需要再查明