Reading a web page in Java IOException Premature EOF

后端 未结 5 610
离开以前
离开以前 2020-12-16 19:10

I am frequently getting a \'Premature EOF\' Exception when reading a web page.

The following is the StackTrace

java.io.IOException: Premature EOF
            


        
5条回答
  •  时光说笑
    2020-12-16 19:34

            StringBuilder sb = new StringBuilder();
    
            try{                
                URL url = new URL(address);
    
                InputStream is = url.openStream();
    
                InputStreamReader isr = new InputStreamReader(is);
    
                BufferedReader in = new BufferedReader(isr);
    
                String str;
    
                while((str = in.readLine()) != null){
    
                    sb.append(str);
    
                    sb.append("\n");
                }
    
                in.close();
                isr.close();
                is.close();
    
                return sb.toString();
    
             }catch(Exception e){
    
                 //OMG....
             }    
    

提交回复
热议问题