夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks

          要用java檢測網絡資源是否可用,我們可以采用以下兩種方法:
          一種方法是調用ping命令,
          如:
          Process process= Runtime.getRuntime().exec("ping 192.168.0.5");
          InputStreamReader return = new InputStreamReader(process.getInputStream());
          LineNumberReader returnData = new LineNumberReader (return);

          String line="";
          while((line=returnData.readLine())!=null){
          System.out.println(line);
          }
          通用對返回數據進行分析,來探測網絡資源的可用性;
          這種方法有一個缺點:就是許多網絡資源是不允許被ping的,從而針對這類資源無法探測。

          (2008-07-28記)





          Dos中的ping 命令能很方便的檢測網絡是否為連通狀態.但是在java中,一直沒有找到好的方法檢來測網絡是否為連通狀態.無奈之余,想出了如下方法:

          package com.roadway.edserver.util;

          import java.awt.Toolkit;
          import java.io.InputStream;
          import java.net.HttpURLConnection;
          import java.net.URL;
          /**
           * @Description:本類開啟一個線程檢測網絡是否連通
           * @Author : 惠萬鵬
           * @Time :2008-1-10
           
          */

          public class NetworkManagement implements Runnable {
              
          private int htmlCodeSize;
              
          private int sleepMillisecond;
              
          private int sleepMillisecondWhenNetWorkUnLinked;
              
          private boolean isSpontaneousNotice;
              
          private static boolean networkIsLinked;
              
          private Thread thread = new Thread(this);
              
          private Toolkit toolkit;
              
          private String[] urls;

              
          public NetworkManagement() {
                  
          this.urls = new String[]{"http://www.baidu.com""http://www.google.cn"};
                  
          this.htmlCodeSize = 50;
                  
          this.sleepMillisecond = 5000;
                  
          this.sleepMillisecondWhenNetWorkUnLinked = 10000;
                  
          this.toolkit = Toolkit.getDefaultToolkit();
                  thread.start();
              }

              
          public void setURLs(String[] urls) {
                  
          if (urls != null && urls.length > 0{
                      
          this.urls = urls;
                  }

              }

              
          public void setHtmlCodeSize(int htmlCodeSize) {
                  
          if (htmlCodeSize > 0{
                      
          this.htmlCodeSize = htmlCodeSize;
                  }

              }

              
          public void isSpontaneousNotice(boolean isSpontaneousNotice) {
                  
          this.isSpontaneousNotice = isSpontaneousNotice;
              }

              
          public void setSleepMillisecont(int sleepMillisecont) {
                  
          if (sleepMillisecont > 100{
                      
          this.sleepMillisecond = sleepMillisecont;
                  }

              }

              
          public void setSleepMillisecondWhenNetWorkUnLinked(int sleepMillisecont) {
                  
          if (sleepMillisecont > 100{
                      
          this.sleepMillisecondWhenNetWorkUnLinked = sleepMillisecont;
                  }

              }

              
          public static boolean IsNetWordLinking() {
                  
          return NetworkManagement.networkIsLinked;
              }


              
          public void run() {
                  
          while (true{
                      
          try {
                          
          this.isNetWorkLinked();
                          
          if (!NetworkManagement.networkIsLinked) {
                              
          this.isPrintMessage(this.isSpontaneousNotice);
                              Thread.sleep(
          this.sleepMillisecondWhenNetWorkUnLinked);
                          }

                          System.out.println(NetworkManagement.IsNetWordLinking());
                          Thread.sleep(
          this.sleepMillisecond);
                      }
           catch (Exception e) {
                      }

                  }

              }


              
          private boolean canGetHtmlCode(String httpUrl) {
                  String htmlCode 
          = "";
                  
          try {
                      InputStream in;
                      URL url 
          = new java.net.URL(httpUrl);
                      HttpURLConnection connection 
          = (HttpURLConnection) url
                              .openConnection();
                      connection 
          = (HttpURLConnection) url.openConnection();
                      connection.setRequestProperty(
          "User-Agent""Mozilla/4.0");
                      connection.connect();
                      in 
          = connection.getInputStream();
                      
          byte[] buffer = new byte[this.htmlCodeSize];
                      in.read(buffer);
                      htmlCode 
          = new String(buffer);
                  }
           catch (Exception e) {
                  }

                  
          if (htmlCode == null || htmlCode.equals("")) {
                      
          return false;
                  }

                  
          return true;
              }


              
          private void isNetWorkLinked() {
                  
          boolean tempIsNetWorkLinked = false;
                  
          for (int urlsCount = 0; urlsCount < this.urls.length; urlsCount++{
                      
          if (this.canGetHtmlCode(this.urls[urlsCount])) {
                          tempIsNetWorkLinked 
          = true;
                          
          break;
                      }

                  }

                  NetworkManagement.networkIsLinked 
          = tempIsNetWorkLinked;
              }

              
          private void isPrintMessage(boolean isPrint) {
                  
          if (isPrint) {
                      toolkit.beep();
                      StringBuffer message 
          = new StringBuffer();
                      message.append(
          "------------->");
                      message.append(
          "網絡中斷, ");
                      message.append(
          this.sleepMillisecondWhenNetWorkUnLinked);
                      message.append(
          " 毫秒后再次檢測!<-------------");
                      System.out.println(message.toString());
                  }


              }

              
          public static void main(String[] args) {
                  NetworkManagement n 
          = new NetworkManagement();
                  n.isSpontaneousNotice(
          true);
              }

          }


          posted on 2007-12-07 19:27 HUIKK 閱讀(5210) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 长寿区| 英德市| 汉中市| 曲阜市| 金昌市| 延长县| 邵阳县| 石首市| 康保县| 昌吉市| 嘉禾县| 佛冈县| 八宿县| 满洲里市| 大渡口区| 清苑县| 松潘县| 乌什县| 宝坻区| 高州市| 浏阳市| 青冈县| 岳西县| 马鞍山市| 南澳县| 盐山县| 沙雅县| 开化县| 彩票| 奉节县| 德安县| 阿合奇县| 弋阳县| 涿鹿县| 大悟县| 汉寿县| 镇巴县| 兴隆县| 青田县| 泰安市| 收藏|