2011年6月26日

          這個類是我在寫一個局域網聊天軟件時封裝的網絡實用工具,剛剛開技術博客,沒啥好發的,先發上來湊活著看看吧。
            1 package com.javaliang.net;
            2 
            3 import java.net.InetAddress;
            4 import java.net.UnknownHostException;
            5 
            6 /**
            7  * 包含網絡編程中比較常用的實用方法
            8  * @author Ice Spring
            9  * @version 
           10  */
           11 public class NetTool {
           12     
           13     /**
           14      * 判斷一個字符串是否為IP地址
           15      * 
           16      * 判斷基于以下準則
           17      * <ul>
           18      * <li>不為空(這包括空指針和空串)
           19      * <li>由被“.”分割的四個大小在0~255之間的數字組成
           20      * </ul>
           21      * @param ipAddress 需要判斷的字符串
           22      * @return 是否是IP地址
           23      */
           24     public static boolean isIpAddress(String ipAddress){
           25         //判斷是否為空,為空則為假
           26         if(ipAddress==null||ipAddress.equals("")){
           27             return false;
           28         }
           29         
           30         String strs[] = ipAddress.replace("."";").split(";");
           31 
           32         //如果經過"."分割后不足四位,返回假
           33         if(strs.length!=4){
           34             return false;
           35         }
           36         int[] add = new int[4];
           37         try{
           38             for(int i = 0 ; i < 4 ; i ++){
           39                 add[i] = Integer.parseInt(strs[i]);
           40                 if(add[i]<0||add[i]>255){
           41                     return false;
           42                 }
           43             }
           44         }catch(Exception e){
           45             return false;
           46         }
           47         
           48         return true;
           49     }
           50     
           51     /**
           52      * 獲取原始數據類型的IP地址:一個長度為4的byte數組,高位存在于getAddress()[0]中
           53      * @param ipAddress 字符串形式的ip地址
           54      * @return 原始IP地址
           55      */
           56     public static byte[] getIpByte(String ipAddress){
           57         byte[] ip = new byte[4];
           58         if(!NetTool.isIpAddress(ipAddress)){
           59             return ip;
           60         }
           61         String strs[] = ipAddress.replace("."";").split(";");
           62         for(int i = 0; i < 4 ; i++){
           63             ip[i] = (byte) Integer.parseInt(strs[i]);
           64         }
           65         return ip;
           66     }
           67     
           68     /**
           69      * 根據原始類型的IP地址獲取其字符串表示形式
           70      * @param ip 原始IP地址:必須是一個長度為4的byte數組,高位存在于getAddress()[0]中
           71      * @return 原始IP地址的字符串表示形式<b>如果轉換失敗會返回空串<b>
           72      */
           73     public static String getIpStr(byte[] ip){
           74         if(ip.length!=4return "";
           75         String str = "";
           76         try {
           77             InetAddress net = InetAddress.getByAddress(ip);
           78             str = net.getHostAddress();
           79         } catch (UnknownHostException e) {
           80         }
           81         return str;
           82     }
           83     
           84     /**
           85      * 根據一個表示IP地址的字符串返回其對應的InetAddress對象
           86      * @param ipAdd IP地址的字符串表示形象
           87      * @return 對應的InetAddress對象,如果發生錯誤則返回空
           88      */
           89     public static InetAddress getInetAddress(String ipAdd){
           90         if(ipAdd == null || !NetTool.isIpAddress(ipAdd)){
           91             return null;
           92         }
           93         byte[] ip = NetTool.getIpByte(ipAdd);
           94         try {
           95             return InetAddress.getByAddress(ip);
           96         } catch (UnknownHostException e) {
           97             e.printStackTrace();
           98             return null;
           99         }
          100     }
          101     
          102     /**
          103      * 根據Ip地址的原始表示形式返回對應的InetAddress對象。
          104      * @param ipadd 以byte數組表示的IP地址
          105      * @return InetAddress 對象
          106      */
          107     public static InetAddress getInetAddress(byte[] ipadd){
          108         try {
          109             return InetAddress.getByAddress(ipadd);
          110         } catch (UnknownHostException e) {
          111             e.printStackTrace();
          112         }
          113         return null;
          114     }
          115     
          116     public static void main(String[] args){
          117         
          118     }
          119 
          120 }
          121 
          posted @ 2011-06-26 23:17 醉三秋 閱讀(64) | 評論 (0)編輯 收藏
          僅列出標題  

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統計

          留言簿

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 宝清县| 白城市| 新建县| 嘉兴市| 沙河市| 景洪市| 九龙坡区| 雷州市| 岳阳市| 区。| 永川市| 宣城市| 富顺县| 辛集市| 弥勒县| 赤城县| 绥德县| 宣化县| 尚义县| 改则县| 曲松县| 张家川| 嵊泗县| 龙南县| 肥西县| 平和县| 会泽县| 北京市| 绥德县| 富宁县| 三穗县| 平昌县| 出国| 阳原县| 五家渠市| 米易县| 卢龙县| 内乡县| 屯留县| 濮阳市| 洪湖市|