简体   繁体   English

用Java存储IP的内容

[英]What to store an IP as in Java

I am connecting a Java program to a MySQL database, so I need the IP to connect it. 我正在将Java程序连接到MySQL数据库,因此需要IP才能连接它。 Hard coding the IP is obviously a bad idea so I made a config. 对IP进行硬编码显然不是一个好主意,因此我进行了配置。 My only problem is I am not sure what to store the IP as in the variable. 我唯一的问题是我不确定在变量中存储IP的内容。 I looked around an a lot of places say int, but I don't see how that can be correct because integers don't have decimals. 我到处逛逛很多地方都说int,但是我看不出这怎么正确,因为整数没有小数。 What should I do? 我该怎么办?

Edit: The answer from Mattias F is more appropriate to your situation. 编辑:Mattias F的答案更适合您的情况。 That being said, I'll explain why you've heard that an IP address can be stored in an integer. 话虽如此,我将解释为什么您听说IP地址可以存储为整数的原因。

An IPv4 address consists of 4 octects, or 4 bytes of data. IPv4地址由4个八位字节或4个字节的数据组成。 Incidentally, that's exactly how much information a 32-bit integer can hold. 顺便说一句,这正是32位整数可以容纳的信息量。 Here are the methods to go both ways: 这是两种方法的结合:

public static int ipStringToInteger (final String ip)
{
    int value = 0;
    final String[] parts = ip.split ("\\.");
    for (final String part : parts)
        value = (value << 8) + Integer.parseInt (part);
    return value;
}
public static String ipIntegerToString (int ip)
{
    final String[] parts2 = new String[4];
    for (int i = 0; i < 4; i++)
    {
        System.out.println ("Value : " + (ip & 0xff));
        parts2[3 - i] = Integer.toString (ip & 0xff);
        ip >>= 8;
    }
    return parts2[0] + '.' + parts2[1] + '.' + parts2[2] + '.' + parts2[3];
}

Edit: Integer.valueOf replace with Integer.parseInt, thanks to Unihedron 编辑:Integer.valueOf替换为Integer.parseInt,这要感谢Unihedron

As this is in a config file I would just store it all as a string for readability, but you can store it as whatever you want. 由于这是在配置文件中,因此我将其全部存储为字符串以提高可读性,但您可以将其存储为所需的任何内容。 If this is your IP: "xxx.xxx.xxx.xxx", I would just save it as it is in your config file. 如果这是您的IP:“ xxx.xxx.xxx.xxx”,我将其保存在您的配置文件中。 Then the rest is dependent on what format your framework for connecting to the database want it on. 然后剩下的取决于您要用于连接到数据库的框架的格式。 If it needs a string, you are done, if you need an int[] you just split on "." 如果它需要一个字符串,那么就完成了;如果您需要一个int [],则只需拆分“”。 and do Integer.parseInt on the result from split. 然后对分割结果执行Integer.parseInt。 Its also possible to have each "xxx" separated with something completely different or even have them on separate lines, but that will both mean more work for you when you parse or lesser readability. 也可以将每个“ xxx”以完全不同的方式分开,甚至将它们放在单独的行上,但这都意味着解析时会为您增加工作量或降低可读性。

store IP in properties file 将IP存储在属性文件中

Message.properties Message.properties

#Mysql server Configuration 
username = root
password = india
ip = 192.168.1.1

MySQLConnection.java MySQLConnection.java

Properties pro = new Properties();
InputStream in = getClass().getResourceAsStream("Message.properties");
pro.load(in);
String userName = pro.getProperty("username");
String password = pro.getProperty("password");
String IP = pro.getProperty("ip");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM