我的一个SWT应用程序,需要每5分钟获取一下新闻,在测试程序时报异常如下,不知是什么原因导致?
异常: No buffer space available (maximum connections reached?): connect public String getNewsContent(){
DataInputStream dis=null;
HttpURLConnection con = null;
String content="";
StringBuffer buffer=new StringBuffer();
try{
String url="http://www.etnetchina.com/EtnetChinaShow/newsList.do;jsessionid=999E61E5BC3DCAE9D840465A7858B792.tomcat2?model=focusNews";
con = (HttpURLConnection)new URL(url).openConnection();
con.setConnectTimeout(30000); //连接超时
con.setReadTimeout(30000); //读操作超时'
System.setProperty("sun.net.client.defaultConnectTimeout","60000");
System.setProperty("sun.net.client.defaultReadTimeout","60000");
con.connect();
dis=new DataInputStream(con.getInputStream());
String line="";
while((line=dis.readLine())!=null){
try{
line=new String(line.trim().getBytes("iso8859-1"),"UTF-8");
buffer.append(line+"\n");
}catch(Exception e){
log.info("while "+e.getMessage());
//throw new RuntimeException(); //抛出运行时异常,上一层进行捕获!
break;
}
}
dis.close();
con.disconnect();
String newsXml=buffer.toString();
log.info("Filtration/Interception...");
int n=newsXml.indexOf("<div class=\"allnewslisttext\"> ");
int l=newsXml.indexOf("</div> ");
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xml=newsXml.substring(n,l);
xml+="</div>";
RssFunction fun=new RssFunction();
fun.writeTextFile(xml);
String date=this.getTime(); DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("D:/SLDDataCenter/center/media/xgtemp.xml"); NodeList list=doc.getElementsByTagName("li");
for(int i=0;i<list.getLength();i++){
Element node=(Element)list.item(i);
String dateTime=node.getElementsByTagName("span").item(0).getFirstChild().getTextContent().trim();
String dateTime_[]=dateTime.split(" ");
if(dateTime_[0].equals(date)){
content+="<img src=\"\" width=\"24\"/>"+dateTime+"<img src=\"\" width=\"8\"/>"+node.getElementsByTagName("a").item(0).getFirstChild().getTextContent().trim();
}
else{
content+="<img src=\"\" width=\"24\"/>"+dateTime+"<img src=\"\" width=\"8\"/>"+node.getElementsByTagName("a").item(0).getFirstChild().getTextContent().trim();
}
}
// log.info("轉換為繁體字!");
ComplexFont font=new ComplexFont();
String complex=font.simpleToComplex(content);
content=null;
content=complex;
fun.deleteFile("D:/SLDDataCenter/center/media/xgtemp.xml");
log.info("news success!");
}catch(ConnectException e){
log.info("NewsRss网站连接不通!");
}catch(NoRouteToHostException e1){
log.info("NewsRss掉线或网络不通异常!");
}catch(UnknownHostException ex){
log.info("NewsRss地址无法解析异常!"+ex.getMessage());
}catch(Exception e){
log.info("Error: NewsRss!"+e.getMessage());
}
finally{
try{
dis.close();
con.disconnect();
}
catch(Exception e){}
}
return content;
}
异常: No buffer space available (maximum connections reached?): connect public String getNewsContent(){
DataInputStream dis=null;
HttpURLConnection con = null;
String content="";
StringBuffer buffer=new StringBuffer();
try{
String url="http://www.etnetchina.com/EtnetChinaShow/newsList.do;jsessionid=999E61E5BC3DCAE9D840465A7858B792.tomcat2?model=focusNews";
con = (HttpURLConnection)new URL(url).openConnection();
con.setConnectTimeout(30000); //连接超时
con.setReadTimeout(30000); //读操作超时'
System.setProperty("sun.net.client.defaultConnectTimeout","60000");
System.setProperty("sun.net.client.defaultReadTimeout","60000");
con.connect();
dis=new DataInputStream(con.getInputStream());
String line="";
while((line=dis.readLine())!=null){
try{
line=new String(line.trim().getBytes("iso8859-1"),"UTF-8");
buffer.append(line+"\n");
}catch(Exception e){
log.info("while "+e.getMessage());
//throw new RuntimeException(); //抛出运行时异常,上一层进行捕获!
break;
}
}
dis.close();
con.disconnect();
String newsXml=buffer.toString();
log.info("Filtration/Interception...");
int n=newsXml.indexOf("<div class=\"allnewslisttext\"> ");
int l=newsXml.indexOf("</div> ");
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xml=newsXml.substring(n,l);
xml+="</div>";
RssFunction fun=new RssFunction();
fun.writeTextFile(xml);
String date=this.getTime(); DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("D:/SLDDataCenter/center/media/xgtemp.xml"); NodeList list=doc.getElementsByTagName("li");
for(int i=0;i<list.getLength();i++){
Element node=(Element)list.item(i);
String dateTime=node.getElementsByTagName("span").item(0).getFirstChild().getTextContent().trim();
String dateTime_[]=dateTime.split(" ");
if(dateTime_[0].equals(date)){
content+="<img src=\"\" width=\"24\"/>"+dateTime+"<img src=\"\" width=\"8\"/>"+node.getElementsByTagName("a").item(0).getFirstChild().getTextContent().trim();
}
else{
content+="<img src=\"\" width=\"24\"/>"+dateTime+"<img src=\"\" width=\"8\"/>"+node.getElementsByTagName("a").item(0).getFirstChild().getTextContent().trim();
}
}
// log.info("轉換為繁體字!");
ComplexFont font=new ComplexFont();
String complex=font.simpleToComplex(content);
content=null;
content=complex;
fun.deleteFile("D:/SLDDataCenter/center/media/xgtemp.xml");
log.info("news success!");
}catch(ConnectException e){
log.info("NewsRss网站连接不通!");
}catch(NoRouteToHostException e1){
log.info("NewsRss掉线或网络不通异常!");
}catch(UnknownHostException ex){
log.info("NewsRss地址无法解析异常!"+ex.getMessage());
}catch(Exception e){
log.info("Error: NewsRss!"+e.getMessage());
}
finally{
try{
dis.close();
con.disconnect();
}
catch(Exception e){}
}
return content;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货