基于Java图形界面的IPV4与网址的地址解析器

时间:2022-07-22
本文章向大家介绍基于Java图形界面的IPV4与网址的地址解析器,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

基于Java图形界面的IPV4与网址的地址解析器

效果图

正则判断IPV4地址

	//正则判断是不是ipv4地址  a.b.c.d
	public static boolean isIPv4(String data) {
	   if (data == null) {
            return false;
	   }
	   
	    Boolean bol = false;
        Pattern pattern = Pattern.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
        bol =  pattern.matcher(data).matches();
        if(bol==true) { //如果格式对了,那就在判断数字的大小
	    	String[] split = data.split("\.");
	    	System.err.println( split.length);
	    	for (int i = 0; i < split.length; i++) {
				String string = split[i];
				System.out.println(string);
				if(Integer.valueOf(string)>244) {
					bol=false;
				}
			}
        }
        
        return bol;
	}

正则判断URL

	//正则判断是不是网址  www.abc.asd、www.a.d.s、q.w.d.c
	public static boolean iswww(String data) {
		if (data == null) {
            return false;
	    }
		//http://  https://  带前缀的
//        Pattern pattern = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$");
        //不带前缀的
		Pattern pattern = Pattern.compile("^(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$");
		
        return pattern.matcher(data).matches();
	}

全部代码

package test02;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class test02 extends JFrame implements ActionListener{

	public static void main(String[] args) {
		new test02();
	}

	
	JLabel j1 = null;//提示
	JTextField jt1 = null;//接收输入的参数
	JButton jb1 = null;//确认按钮
	JTextArea jta = null;//显示结果的文本域
	
	
	public test02() {
		this.setTitle("地址域名解析器-v0.2");
		this.setLayout(null);
		j1 = new JLabel("请输入您要解析的IPV4地址或者域名:");
		jt1 = new JTextField(100);
		jb1 = new JButton("解析");
		jb1.addActionListener(this);
		jta = new JTextArea();
		this.add(j1);
		this.add(jt1);
		this.add(jb1);
		this.add(jta);
		j1.setBounds(30, 10, 250, 30);
		jt1.setBounds(303, 10, 300, 30);
		jb1.setBounds(630, 10, 100, 30);
		jta.setBounds(30, 80, 630, 200);
		this.setSize(800, 400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == jb1) {
			String data = jt1.getText();//获取用户输入的数据	
			if(isIPv4(data)==true) {
				try {
					InetAddress[] allByName = InetAddress.getAllByName(data);
					StringBuffer str = new StringBuffer();
					jta.setText("解析成功,结果:"+(new Date().toString())+"n");
					for (InetAddress net:allByName) {
						jta.append(String.valueOf(net)+"n");
					}
				} catch (UnknownHostException e1) {
					System.out.println("出错了:"+e1.getMessage()+"不正确");
					jta.setText("出错了:"+e1.getMessage()+"不正确");
				}
			}else if(iswww(data)==true) {
				try {
					InetAddress[] allByName = InetAddress.getAllByName(data);
					StringBuffer str = new StringBuffer();
					jta.setText("解析成功,结果:"+(new Date().toString())+"n");
					for (InetAddress net:allByName) {
						jta.append(String.valueOf(net)+"n");
					}
				} catch (UnknownHostException e1) {
					System.out.println("出错了:"+e1.getMessage()+"不正确");
					jta.setText("出错了:"+e1.getMessage()+"不正确");
				}
			}else {
				jta.setText("您输入的数据不合法,请检查后重新数据!");
			}
		}
		
	}
	
	//正则判断是不是网址  www.abc.asd、www.a.d.s、q.w.d.c
	public static boolean iswww(String data) {
		if (data == null) {
            return false;
	    }
		//http://  https://  带前缀的
//        Pattern pattern = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$");
        //不带前缀的
		Pattern pattern = Pattern.compile("^(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$");
		
        return pattern.matcher(data).matches();
	}
	

	//正则判断是不是ipv4地址  a.b.c.d
	public static boolean isIPv4(String data) {
	   if (data == null) {
            return false;
	   }
	   
	    Boolean bol = false;
        Pattern pattern = Pattern.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
        bol =  pattern.matcher(data).matches();
        if(bol==true) { //如果格式对了,那就在判断数字的大小
	    	String[] split = data.split("\.");
	    	System.err.println( split.length);
	    	for (int i = 0; i < split.length; i++) {
				String string = split[i];
				System.out.println(string);
				if(Integer.valueOf(string)>244) {
					bol=false;
				}
			}
        }
        
        return bol;
	}
	
	
	
}