电竞比分网-中国电竞赛事及体育赛事平台

分享

用 java 檢測主機連網(wǎng)狀態(tài)

 昵稱11462324 2013-01-14

用 java 檢測主機連網(wǎng)狀態(tài)


通過幾天努力終于把那個問題給解決了,就是用java 檢測本機的連網(wǎng)狀態(tài),當網(wǎng)絡(luò)中斷時讓檢測網(wǎng)絡(luò),如果連接上網(wǎng)絡(luò),便又繼續(xù)下面的工作.以下是我寫的一個類,朋友們可以參考一 下                                                                                        清單一:URLAvailability.java

  1. package xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
  2.   
  3. import java.net.HttpURLConnection;  
  4. import java.net.URL;  
  5. import java.net.UnknownHostException;  
  6. import java.text.DateFormat;  
  7. import java.util.Date;  
  8.   
  9. import org.apache.log4j.Logger;  
  10.   
  11. /** 
  12.  *  
  13.  * 項目名稱: xxxxxxxxxx* <p> 
  14.  * 功能模塊名稱: 
  15.  * </p><p> 
  16.  * 文件名稱為:URLAvailability.java 
  17.  * </p><p> 
  18.  * 文件功能簡述: xxxxxxxxxxxxxxxxxxxxxxxxxx * </p><p> 
  19.  * 文件創(chuàng)建人:ChenTao 
  20.  * @author ChenTao 
  21.  * @version v1.0 
  22.  * @time 2008-5-31上午10:00:35 
  23.  * @copyright xxxxxxxxxxxxx */  
  24. @SuppressWarnings("unused")  
  25. public class URLAvailability   {  
  26.   
  27.     private static Logger logger = Logger.getLogger(URLAvailability.class);  
  28.   
  29.     private static URL urlStr;  
  30.   
  31.     private static HttpURLConnection connection;  
  32.   
  33.     private static int state = -1;  
  34.   
  35.     private static String succ;  
  36.   
  37.     private static boolean isCon = false;  
  38.   
  39.     private String url;  
  40.       
  41.     private String closeTime = null;  
  42.       
  43.     private int status = 0;  
  44.   
  45.     public String getUrl() {  
  46.         return url;  
  47.     }  
  48.   
  49.     public void setUrl(String url) {  
  50.         this.url = url;  
  51.     }  
  52.   
  53.     public static String getSucc() {  
  54.         return succ;  
  55.     }  
  56.   
  57.     public static void setSucc(String succ) {  
  58.         URLAvailability.succ = succ;  
  59.     }  
  60.   
  61.     /** 
  62.      * 功能描述 : 檢查URL是否可用 
  63.      *  
  64.      * @param url 
  65.      *            指定檢查的網(wǎng)絡(luò)地址 
  66.      *  
  67.      * @return String 
  68.      */  
  69.     public synchronized static String isConnect(String url) {  
  70.         state = -1;  
  71.         succ = null;  
  72.         if (url == null || url.length() <= 0) {  
  73.             return succ;  
  74.         }  
  75.         new URLAvailability().connectState(url);  
  76.         return succ;  
  77.     }  
  78.   
  79.     /** 
  80.      * 功能描述 : 檢測當前網(wǎng)絡(luò)是否斷開 或 URL是否可連接, 
  81.      *          如果網(wǎng)絡(luò)沒斷開,最多連接網(wǎng)絡(luò) 5 次, 如果 5 次都不成功說明該地址不存在或視為無效地址. 
  82.      *  
  83.      * @param url 
  84.      *            指定URL網(wǎng)絡(luò)地址 
  85.      *  
  86.      * @return void 
  87.      */  
  88.     private synchronized void connectState(String url) {  
  89.         this.url = url;  
  90.         int counts = 0;  
  91.           
  92.         while (counts < 5) {  
  93.             try {  
  94.                 urlStr = new URL(this.getUrl());  
  95.                 connection = (HttpURLConnection) urlStr.openConnection();  
  96.                 state = connection.getResponseCode();  
  97.                 if (state == 200) {  
  98.                     succ = connection.getURL().toString();  
  99.                 }  
  100.                 break;  
  101.             } catch (UnknownHostException ex) {  
  102.                 if(this.closeTime == null){  
  103.                     DateFormat df = DateFormat.getDateTimeInstance();  
  104.                     closeTime = df.format(new Date());  
  105.                     logger.error("網(wǎng)絡(luò)連接狀態(tài)已斷開,請檢查網(wǎng)絡(luò)連接設(shè)備");  
  106.                     logger.info("斷開時間 : " + this.closeTime);  
  107.                     logger.error("程序開始執(zhí)行每三分鐘檢測一次網(wǎng)絡(luò)");  
  108.                 }  
  109.                 try {  
  110.                     status ++;  
  111.                     logger.info("開始第" + status + " 次檢測網(wǎng)絡(luò)狀態(tài)是否可連接");  
  112.                     Thread.sleep(180000);  
  113.                 } catch (InterruptedException e) {  
  114.                 }  
  115.                 this.connectState(this.getUrl());  
  116.             } catch (Exception ex) {  
  117.                 counts++;  
  118.                 continue;  
  119.             }  
  120.             if(status != 0){  
  121.   
  122.                 DateFormat df = DateFormat.getDateTimeInstance();  
  123.                 closeTime = df.format(new Date());  
  124.                 logger.info("網(wǎng)絡(luò)成功連接");  
  125.             }  
  126.         }  
  127.     }  
  128. }  
  129. 大家看到上面我 cache 了UnknownHostException  異常,這個意思是在調(diào)用遠程主機發(fā)生的異常,我們只需要 cache 這個異常就搞定了,不信可試試把網(wǎng)線斷開后看看會發(fā)生什么

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章