Java 获取公网ip地址或内网地址
public String getIPAddress(){  
    try {  
        URL url = new URL("http://checkip.amazonaws.com/");  
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));  
        String ip = br.readLine().trim();  
        System.out.println("Public IP Address: " + ip);  
        return ip;  
    } catch (Exception e) {  
        System.out.println("Error in getting public IP address, trying to get local IP address.");  
        try {  
            String localIP = getLocalIP();  
            System.out.println("Local IP Address: " + localIP);  
        } catch (SocketException se) {  
            System.out.println("Error in getting local IP address: " + se.getMessage());  
        }  
    }  
    return null;  
}  
  
private static String getLocalIP() throws SocketException {  
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();  
    while (networkInterfaces.hasMoreElements()) {  
        NetworkInterface networkInterface = networkInterfaces.nextElement();  
        Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();  
        while (inetAddresses.hasMoreElements()) {  
            InetAddress inetAddress = inetAddresses.nextElement();  
            if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {  
                return inetAddress.getHostAddress();  
            }  
        }  
    }  
    return null;  
}

Pasted image 20240217210709.png

无网络环境则获取内网地址

评论

  1. Windows Chrome
    3 天前
    2026-5-13 6:59:29

    Honestly, the depth here is unexpected. I was skeptical about mobile casino platforms, but the XGames APK overview is quite detailed. For anyone serious about testing these systems, check out xgames apk games first. Impressive technical write-up!

  2. Macintosh Chrome
    2 月前
    2026-3-31 23:07:04

    This code elegantly handles the dual challenge of IP detection! The fallback strategy from public to local IP is particularly robust for distributed systems. Similar to how platforms like 2jl app casino rely on accurate IP detection for geo-targeting and security, your implementation provides reliable network awareness for Java applications. Great solution!

  3. Windows Chrome
    3 月前
    2026-3-01 2:36:05

    Excellent approach to IP address handling! The fallback mechanism from public to local IP is smart. This reliability pattern is crucial in app development – similar to how we ensure seamless connectivity at jilibb app download apk. Clean code structure and proper exception handling make this production-ready.

  4. Windows Firefox
    4 月前
    2026-1-17 16:30:40

    Really insightful article! It’s great to see platforms like bigbunny prioritizing responsible gaming alongside competitive features & robust verification-essential for a healthy experience. Strategic account setup is key!

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇