首开技术Blog,上点小礼-NetTool

这个类是我在写一个局域网聊天软件时封装的网络实用工具,刚刚开技术博客,没啥好发的,先发上来凑活着看看吧。
  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 on 2011-06-26 23:17 醉三秋 阅读(65) 评论(0)  编辑  收藏 所属分类: JavaSE


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

统计

留言簿

文章分类

文章档案

搜索

最新评论