code:
try {
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println("ip地址是:"+localAddress.getHostAddress());
System.out.println("主机名是"+localAddress.getHostName());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
不在域中上面的代码是没有什么问题的,可是到了域里就不好用了!
还有一个问题,在域中调用request.getRemoteAddr()为什么得不到本地本地ip地址啊,而是得到域的ip,很是让我头疼,域是什么概念啊?

解决方案 »

  1.   

    在cmd下面使用ipconfig /all,看一下你的IP地址。
    import java.net.*;
    public class IpTest {
    public static void main(String[] args) {
    try {
    InetAddress localAddress = InetAddress.getLocalHost();
    System.out.println("ip地址是:"+localAddress.getHostAddress());
    System.out.println("主机名是"+localAddress.getHostName());
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    }
    我的程序运行结果
    ip地址是:218.198.115.62
    主机名是zz
    Press any key to continue...你还可以用Process p = Runtime.getRuntime("ipconfig /all"),获得输入流后,然后处理可以获得你的IP地址。
      

  2.   


    这是以前某位GG写的,现在贴出来你看看吧,可以获得IP地址的。
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2006</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    public class MACAddress {
        public MACAddress() {
        }    public static String getMACAddress() {        String address = "";
            String os = System.getProperty("os.name");
            if (os != null && os.startsWith("Windows")) {
                try {
                    String command = "cmd.exe /c ipconfig /all";
                    Process p = Runtime.getRuntime().exec(command);
                    BufferedReader br =
                            new BufferedReader(
                                    new InputStreamReader(p.getInputStream()));
                    String line;
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf("IP Address") > 0) {
                            int index = line.indexOf(":");
                            index += 2;
                            address = line.substring(index);
                            break;
                        }
                    }
                    br.close();
                    return address.trim();
                } catch (IOException e) {}
            }
            return address;
        } public static void main(String[] args) {
        System.out.println(""+MACAddress.getMACAddress());
    }
    }
      

  3.   

    你自己安装一个2000server你就懂什么是域了。