简体   繁体   English

编码ipv4地址以在Java中使用

[英]Encoding ipv4 Address for use in Java

I'm using PMD to check java code, and I've run into the problem that states, "Do not hard code IPv4 or IPv6 addresses, even 127.0.0.1!" 我正在使用PMD来检查Java代码,但遇到了一个问题:“请勿硬编码IPv4或IPv6地址,甚至127.0.0.1!” The IPv4 address I'm using is in fact just 127.0.0.1, and is only for testing purposes, but nonetheless I must convert the hard-coded version to some sort of encrypted version. 我使用的IPv4地址实际上只是127.0.0.1,仅用于测试目的,但是我仍然必须将硬编码版本转换为某种加密版本。 I'm not sure what would be the easiest way to do this. 我不确定最简单的方法是什么。

Any help would be greatly appreciated! 任何帮助将不胜感激!

You are not trying to encrypt anything. 您没有尝试加密任何东西。

What you wan to do is pass in a host name and do the proper host lookup to get the Internet address. 您要做的就是传递一个主机名,并进行适当的主机查找以获取Internet地址。 Look at the standard JDK's InetAddress and the getAllByName(String host) and getByName(String host) 查看标准JDK的InetAddress以及getAllByName(String host)和getByName(String host)

I've resolved this PMD´s warning in this way: 我已通过以下方式解决了此PMD的警告:


// Old code
`if (!"127.0.0.1".equals(serverIP)) { ... }`

// New code
`if (!InetAddress.getLoopbackAddress().getHostAddress().equals(serverIP)) { ... }`

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

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