try to use StringBuffer pageContext instead of String.

解决方案 »

  1.   

    你没有关闭连接. 加代码
     finally {
        if(stdIn !=null) try { stdIn .close(); } catch(IOException e) {} 
     }
      

  2.   

    呵呵,连接关闭了的,整个代码是:
      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());
                    }
                }
            }
      

  3.   

    贴出整个方法吧:
    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;
        }
      

  4.   

    String strHtml = "";
    StringBuffer sb = new StringBuffer(pageContent);
    while((strHtml = stdIn.readLine())!=null) {
      sb.append(strHtml).append("\r\n");
    }pageContent = sb.toString();但是如果最后pageContent太大了,很容易会出现OutOfMemory的。小心
      

  5.   

    呵呵,关于改成StringBuffer各位能说说原因吗?谢谢了
      

  6.   

    你用string + 的话每次加上一个字符串都会生成一个新的对象。耗费空间。
      

  7.   

    我试了一下,没有出现你说的问题,叶面内容很快出来了,我试了一下sina首页,大概5秒钟,改为StringBuffer,大概2秒
      

  8.   

    e.getMessage()这个方法最好在实际开发中不要使用,很多时候,这个方法不会返回任何东西,
      

  9.   

    如果你的是一个HTTP请求后返回的流的确可能存在一个问题:HTTP并不是每次请求完成以后都必须关闭这个连接,而只是在HTTP头上面有一个
    Content-Length: 12345    来声明返回结果的大小,
    结果在后面的数据段中,它会返回12345个字节然后就停止不动了,因为这个时候其实你是可以再通过这个连接来发送下一次请求,所以readLine就总是不能返回!这个机制是为了提高效率而做的,如果你需要避免这种情况,在发送请求HTTP头的时候就必须增加以下一行:
    Connection: close这样对方就会在发送完成数据以后自动关闭这个连接,这样readLine就会退出了。
    以上问题只是一种可能性,具体原因你还需要再查明
      

  10.   

    请问加一行Connection: close具体应该怎么操作?