java的一些实用工具方法(用的时候随手了)

本文介绍了如何将IP地址从点分十进制格式转换为长整型数值,并验证其有效性,还包括判断IP地址是否属于内网地址的方法。此外,还提供了一个示例,展示如何将ActionForm中的数据绑定到XML文件中对应的元素上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 将127.0.0.1 形式的IP地址转换成10进制整数,这里没有进行任何错误处理
	public static long ipToLong(String strIP) {
		long[] ip = new long[4];
		// 先找到IP地址字符串中.的位置
		int position1 = strIP.indexOf(".");
		int position2 = strIP.indexOf(".", position1 + 1);
		int position3 = strIP.indexOf(".", position2 + 1);
		// 将每个.之间的字符串转换成整型
		ip[0] = Long.parseLong(strIP.substring(0, position1));
		ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2));
		ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3));
		ip[3] = Long.parseLong(strIP.substring(position3 + 1));
		return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
	}

 ********************适当的分割线****************************

 

/**
	 * 判断ip地址的合法性
	 */
	public static boolean isValidIPAddress(String str) {
		String temp = "";
		int tag = 0;
		//字符串的第一位和最后以为如果是.的话返回false
		if (str.charAt(0) == '.' || str.charAt(str.length() - 1) == '.')
			return false;
		for (int i = 0; i < str.length(); i++) {
			System.out.println("temp ==="+temp);
			if (str.charAt(i) == '.') {
				System.out.println("tag1 === "+tag);
				tag++;
				System.out.println("tag2 === "+tag);
				
				if (Integer.parseInt(temp) > 255)
					return false;
				temp = "";
				continue;
			}
			if (str.charAt(i) < '0' || str.charAt(i) > '9')
				return false;
			temp += String.valueOf(str.charAt(i));
		}
		System.out.println("tag3 =="+tag);
		if (tag != 3)
			return false;
		return true;
	}

 

********************适当的分割线****************************

 

 

public static int isInnerIP(long a_ip)// 检查ip地址是否是内网ip
	{
		int bValid = -1;
		if ((a_ip >> 24 == 0xa) || (a_ip >> 16 == 0xc0a8)
				|| (a_ip >> 22 == 0x2b0)) {
			bValid = 0;
		}
		return bValid;
	}

 

********************适当的分割线****************************

 

//	通过参数将ActionForm中的数据和xml文件中的值绑定可以进行读写操作..
	public boolean setFormDataToXML(ConfigVrrpParametersForm vrrpForm,
			String xml, String root) {
		File xmlFile = new File(xml);
		boolean b = false;
		// String root = "configVRRP";
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			// b = false;
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(xmlFile);
			NodeList nodelist = doc.getElementsByTagName(root);
			int size = nodelist.getLength();
			for (int i = 0; i < size; i++) {
				Node n = nodelist.item(i);
				NodeList nl2 = n.getChildNodes();
				int size2 = nl2.getLength();
				for (int j = 0; j < size2; j++) {
					Node n2 = nl2.item(j);
					if (n2.hasChildNodes()) {
						String name = n2.getNodeName();
						// String value = n2.getFirstChild().getNodeValue();
						Node value = n2.getFirstChild();
						if (name.equals("vrrp_Type")) {
							String formValue = vrrpForm.getVrrp_Type();
							if (null != formValue) {
								value.setNodeValue(formValue);
							}else{
								value.setNodeValue("");
							}
								
						}
						if (name.equals("ha_Monitoring_interface")) {
							String formValue = vrrpForm
									.getHa_Monitoring_interface();
							if (null != formValue) {
								value.setNodeValue(formValue);
							}else{
								value.setNodeValue("");
							}
						}
						if (name.equals("vistual_IP")) {
							String formValue = vrrpForm.getVistual_IP();
							if (null != formValue) {
								value.setNodeValue(formValue);
							}else{
								value.setNodeValue("");
							}

						}
						if (name.equals("vistual_route_ID")) {
							String formValue = vrrpForm.getVistual_route_ID();
							if (null != formValue) {
								value.setNodeValue(formValue);
							}else{
								value.setNodeValue("");
							}
						}
						if (name.equals("priority_Value")) {
							String formValue = vrrpForm.getPriority_Value();
							if (null != formValue) {
								value.setNodeValue(formValue);
							}else{
								value.setNodeValue("");
							}
						}
						if (name.equals("auth_Type")) {
							String formValue = vrrpForm.getAuth_Type();
							if (null != formValue) {
								value.setNodeValue(formValue);
							}else{
								value.setNodeValue("");
							}
						}
						if (name.equals("auth_Pass")) {
							String formValue = vrrpForm.getAuth_Pass();
							if (null != formValue) {
								value.setNodeValue(formValue);
							}else{
								value.setNodeValue("");
							}
						}
					}
				}
				outputXmlFile(n,xml);
				b = true;
			}
		} catch (ParserConfigurationException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (SAXException ex) {
			ex.printStackTrace();
		}
		return b;
	}

	public static void outputXmlFile(Node node,String outxmlFile) {
		// 根据节点生成xml文件的
		TransformerFactory transFactory = TransformerFactory.newInstance();
		try {
			Transformer transformer = transFactory.newTransformer();
			transformer.setOutputProperty("encoding", "utf-8");
			transformer.setOutputProperty("indent", "yes");

			DOMSource source = new DOMSource();
			if (source != null) {
				if (node != null) {
					source.setNode(node);
				}
				StreamResult result = new StreamResult(outxmlFile);
				File a = new File("c:\\configVRRPym.xml");
				// result.setOutputStream(System.out);
				transformer.transform(source, result);
			}
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值