// 将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();
}
}