我想从我的java applet中确定本地IP地址.问题是当同一台机器上有多个IP地址时,它们有LAN和互联网连接(掌上电脑,VMWare ......).
这是我的测试:
public static void main(String[] args) {
try {
String hostName = InetAddress.getLocalHost().getHostName();
System.out.println("HostName = " + hostName);
System.out.println("HostAddressLocal = " +
InetAddress.getLocalHost().getHostAddress());
InetAddress[] inetAddresses = InetAddress.getAllByName(hostName);
for (InetAddress inetAddress : inetAddresses) {
System.out.println("hostAddress = " + inetAddress.getHostAddress());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
HostName = xxxx
HostAddressLocal = xx.xx.xx.xx
hostAddress = 10.10.11.51
hostAddress = 192.168.23.1
hostAddress = 192.168.106.1
Run Code Online (Sandbox Code Playgroud)
其中xx.xx.xx.xx不是正确的地址.正确的是10.10.11.51.
编辑以回应jarnbjo:
你的水晶球说实话.你了解我的问题.客户端可以通过代理连接,因此我无法使用您的第一点.如果我在我的电脑上执行以下代码:
Socket s = new Socket("www.w3c.org", 80);
InetAddress ip = …Run Code Online (Sandbox Code Playgroud) 我想在pom.xml中转义一个属性.不是在资源中,我知道可以使用过滤器.例如,我尝试使用launch4j插件,如下所示:
<plugin>
<groupId>org.bluestemsoftware.open.maven.plugin</groupId>
<artifactId>launch4j-plugin</artifactId>
<executions>
<execution>
<id>l4j-cli</id>
<phase>install</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>console</headerType>
<outfile>../out.exe</outfile>
<dontWrapJar>true</dontWrapJar>
<jar>./../out.jar</jar>
<icon>../icon.ico</icon>
<chdir>.</chdir>
<customProcName>true</customProcName>
<downloadUrl>http://www.oracle.com/technetwork/java/javase/downloads/index.html</downloadUrl>
<classPath>
<mainClass>com.stack.Main</mainClass>
<addDependencies>true</addDependencies>
<jarLocation>./lib</jarLocation>
</classPath>
<jre>
<opts>
<opt>-DconfigBasePath=${ALLUSERSPROFILE}/dir</opt>
</opts>
</jre>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
并且$ {ALLUSERSPROFILE}不能由maven解释,而是由launch4j生成的程序.我尝试:
\${ALLUSERSPROFILE}
\\${ALLUSERSPROFILE}
$${ALLUSERSPROFILE}
Run Code Online (Sandbox Code Playgroud)
和
<properties>
<dollar>$</dollar>
</properties>
${dollar}{ALLUSERSPROFILE}
Run Code Online (Sandbox Code Playgroud)
但没什么用.