精华内容
下载资源
问答
  • Java获取ip地址

    2021-04-15 14:58:03
    Java获取ip地址 package com.sinosoft.ireport; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util....

    Java获取ip地址

    package com.sinosoft.ireport;
    
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Enumeration;
    import javax.servlet.http.HttpServletRequest;
    
    
    /**
     * @program: Report
     * @description: 获取IP地址
     * @author: XWJ
     * @create: 2021-04-14 21:03
     **/
    public class WebToolUtils {
    
        /**
         * 获取本地IP地址
         *
         * @throws SocketException
         */
        public static String getLocalIP() throws UnknownHostException, SocketException {
            if (isWindowsOS()) {
                return InetAddress.getLocalHost().getHostAddress();
            } else {
                return getLinuxLocalIp();
            }
        }
    
        /**
         * 判断操作系统是否是Windows
         *
         * @return
         */
        public static boolean isWindowsOS() {
            boolean isWindowsOS = false;
            String osName = System.getProperty("os.name");
            if (osName.toLowerCase().indexOf("windows") > -1) {
                isWindowsOS = true;
            }
            return isWindowsOS;
        }
    
        /**
         * 获取本地Host名称
         */
        public static String getLocalHostName() throws UnknownHostException {
            return InetAddress.getLocalHost().getHostName();
        }
    
        /**
         * 获取Linux下的IP地址
         *
         * @return IP地址
         * @throws SocketException
         */
        private static String getLinuxLocalIp() throws SocketException {
            String ip = "";
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                    NetworkInterface intf = en.nextElement();
                    String name = intf.getName();
                    if (!name.contains("docker") && !name.contains("lo")) {
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress()) {
                                String ipaddress = inetAddress.getHostAddress().toString();
                                if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
                                    ip = ipaddress;
                                    System.out.println(ipaddress);
                                }
                            }
                        }
                    }
                }
            } catch (SocketException ex) {
                System.out.println("获取ip地址异常");
                ip = "127.0.0.1";
                ex.printStackTrace();
            }
            System.out.println("IP:" + ip);
            return ip;
        }
    
        /**
         * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
         * <p>
         * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?
         * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
         * <p>
         * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130,
         * 192.168.1.100
         * <p>
         * 用户真实IP为: 192.168.1.110
         *
         * @param request
         * @return
         */
        public static String getIpAddress(HttpServletRequest request) {
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip;
        }
    
    }
    
    展开全文
  • java获取ip地址

    2020-06-09 15:48:53
    package com.mbyte.easy.util; import java.net.Inet4Address; import java.net.InetAddress;...* 获取内外网ip地址 * @author Young * @version 2017/5/10 * @email yangxiaoping@freetek.cc */ public c...

    package com.mbyte.easy.util;

    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Enumeration;

    /**
     * 获取内外网ip地址
     * @author Young
     * @version 2017/5/10
     * @email yangxiaoping@freetek.cc
     */
    public class IpAddressUtils {
        public static final String INTRANET_IP = getIntranetIp(); // 内网IP
        public static final String INTERNET_IP = getInternetIp(); // 外网IP
        public static final byte[] INTERNET_IP_BYTES = getInternetIpBytes();//外网ip字节数组
        public static final byte[] INTRANET_IP_BYTES = getIntranetIpBytes(); // 内网IP字节数组

        private IpAddressUtils(){}

        /**
         * 获得内网IP
         * @return 内网IP
         */
        private static String getIntranetIp(){
            try{
                return InetAddress.getLocalHost().getHostAddress();
            } catch(Exception e){
                throw new RuntimeException(e);
            }
        }

        /**
         * 获得外网IP
         * @return 外网IP
         */
        private static String getInternetIp(){
            try{
                Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
                InetAddress ip = null;
                Enumeration<InetAddress> addrs;
                while (networks.hasMoreElements())
                {
                    addrs = networks.nextElement().getInetAddresses();
                    while (addrs.hasMoreElements())
                    {
                        ip = addrs.nextElement();
                        if (ip != null
                                && ip instanceof Inet4Address
                                && ip.isSiteLocalAddress()
                                && !ip.getHostAddress().equals(INTRANET_IP))
                        {
                            return ip.getHostAddress();
                        }
                    }
                }

                // 如果没有外网IP,就返回内网IP
                return INTRANET_IP;
            } catch(Exception e){
                throw new RuntimeException(e);
            }
        }

        /**
         * 获得外网IP
         * @return 外网IP
         */
        private static byte[] getInternetIpBytes(){
            return ipv4Address2BinaryArray(getInternetIp());
        }

        /**
         * 获取内网ip
         * @return
         */
        private static byte[] getIntranetIpBytes() {
            return ipv4Address2BinaryArray(getIntranetIp());
        }

        /**
         * 将给定的用十进制分段格式表示的ipv4地址字符串转换成字节数组
         * @param ipAdd
         * @return
         */
        private static byte[] ipv4Address2BinaryArray(String ipAdd){
            byte[] binIP = new byte[4];
            String[] strs = ipAdd.split("\\.");
            for(int i=0;i<strs.length;i++){
                binIP[i] = (byte) Integer.parseInt(strs[i]);
            }
            return binIP;
        }

        public static void main(String[] args) {
            String internetIp = getInternetIp();
            System.out.println(internetIp);
            String intranetIp = getIntranetIp();
            byte[] bytes = ipv4Address2BinaryArray(intranetIp);
            for (byte b:bytes){
                System.out.println(b);
            }
            System.out.println(intranetIp);
        }

    }

    展开全文
  • a.net.Inet4Address;...import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; public class IpUtils { private static final String[] HEADERS = { "X-F

    java IP地址工具类,java IP地址获取,java获取客户端IP地址

     

    ================================

    ©Copyright 蕃薯耀 2019年1月11日

    http://fanshuyao.iteye.com/

     

    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Enumeration;
    
    import javax.servlet.http.HttpServletRequest;
    
    public class IpUtils {
    
    	private static final String[] HEADERS = { 
            "X-Forwarded-For",
            "Proxy-Client-IP",
            "WL-Proxy-Client-IP",
            "HTTP_X_FORWARDED_FOR",
            "HTTP_X_FORWARDED",
            "HTTP_X_CLUSTER_CLIENT_IP",
            "HTTP_CLIENT_IP",
            "HTTP_FORWARDED_FOR",
            "HTTP_FORWARDED",
            "HTTP_VIA",
            "REMOTE_ADDR",
            "X-Real-IP"
    	};
    	
    	/**
    	 * 判断ip是否为空,空返回true
    	 * @param ip
    	 * @return
    	 */
    	public static boolean isEmptyIp(final String ip){
            return (ip == null || ip.length() == 0 || ip.trim().equals("") || "unknown".equalsIgnoreCase(ip));
        }
    	
    	
    	/**
    	 * 判断ip是否不为空,不为空返回true
    	 * @param ip
    	 * @return
    	 */
    	public static boolean isNotEmptyIp(final String ip){
            return !isEmptyIp(ip);
        }
    	
    	/***
         * 获取客户端ip地址(可以穿透代理)
         * @param request HttpServletRequest
         * @return
         */
        public static String getIpAddress(HttpServletRequest request) {
        	String ip = "";
        	for (String header : HEADERS) {
                ip = request.getHeader(header);
                if(isNotEmptyIp(ip)) {
                	 break;
                }
            }
            if(isEmptyIp(ip)){
            	ip = request.getRemoteAddr();
            }
            if(isNotEmptyIp(ip) && ip.contains(",")){
            	ip = ip.split(",")[0];
            }
            if("0:0:0:0:0:0:0:1".equals(ip)){
                ip = "127.0.0.1";
            }
            return ip;
        }
    	
        
        /**
    	 * 获取本机的局域网ip地址,兼容Linux
    	 * @return String
    	 * @throws Exception
    	 */
    	public String getLocalHostIP() throws Exception{
    		Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
    		String localHostAddress = "";
    		while(allNetInterfaces.hasMoreElements()){
    			NetworkInterface networkInterface = allNetInterfaces.nextElement();
    			Enumeration<InetAddress> address = networkInterface.getInetAddresses();
    			while(address.hasMoreElements()){
    				InetAddress inetAddress = address.nextElement();
    				if(inetAddress != null && inetAddress instanceof Inet4Address){
    					localHostAddress = inetAddress.getHostAddress();
    				}
    			}
    		}
    		return localHostAddress;
    	}
    	
    	
    }

     

     

    ================================

    ©Copyright 蕃薯耀 2019年1月11日

    http://fanshuyao.iteye.com/

    展开全文
  • JAVA获取IP

    2021-02-22 09:36:01
    获取本地IP 代码如下: import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util....

    代码如下:

    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import javax.servlet.http.HttpServletRequest;
    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;
    
    public class IPUtils {
    
        private static String LOCAL_IP = "127.0.0.1";
        private static String UNKOWN = "unknown";
    
        /**
         * 通过HttpServletRequest获取客户端IP
         *
         * @return
         */
        public static String getRequestIP() {
    
            ServletRequestAttributes requestAttributes =
                    (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            //X-Forwarded-For:Squid 服务代理
            String ip = request.getHeader("x-forwarded-for");
    
            if ((ip == null) || (ip.length() == 0) || (UNKOWN.equalsIgnoreCase(ip))) {
                //Proxy-Client-IP:apache 服务代理
                ip = request.getHeader("Proxy-Client-IP");
            }
            if ((ip == null) || (ip.length() == 0) || (UNKOWN.equalsIgnoreCase(ip))) {
                //WL-Proxy-Client-IP:weblogic 服务代理
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if ((ip == null) || (ip.length() == 0) || (UNKOWN.equalsIgnoreCase(ip))) {
                //HTTP_CLIENT_IP:有些代理服务器
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if ((ip == null) || (ip.length() == 0) || (UNKOWN.equalsIgnoreCase(ip))) {
                //X-Real-IP:nginx服务代理
                ip = request.getHeader("X-Real-IP");
            }
            //有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
            if (ip != null && ip.length() != 0) {
                ip = ip.split(",")[0];
            }
            //还是不能获取到,最后再通过request.getRemoteAddr()获取
            if ((ip == null) || (ip.length() == 0) || (UNKOWN.equalsIgnoreCase(ip))) {
                ip = request.getRemoteAddr();
            }
    
            if ("0:0:0:0:0:0:0:1".equals(ip)) ip = LOCAL_IP;
    
            return ip;
        }
    
    
        /**
         * 获取本地IP
         *
         * @return
         */
        public static String getLocalIP() {
            List<String> ips = getLocalIPList();
            for (String ip : ips) {
                if (!LOCAL_IP.equals(ip)) {
                    return ip;
                }
            }
            return LOCAL_IP;
        }
    
        public static List<String> getLocalIPList() {
            try {
                List<String> list = new ArrayList<String>();
    
                Enumeration<?> nis = NetworkInterface.getNetworkInterfaces();
    
                while (nis.hasMoreElements()) {
                    NetworkInterface ni = (NetworkInterface) nis.nextElement();
                    Enumeration<InetAddress> addrs = ni.getInetAddresses();
                    getIpList(list, addrs);
                }
    
                return list;
            } catch (SocketException e) {
                throw new RuntimeException("获得IP地址失败", e);
            }
        }
    
        private static void getIpList(List<String> list, Enumeration<InetAddress> addrs) {
            while (addrs.hasMoreElements()) {
                InetAddress addr = (InetAddress) addrs.nextElement();
                if ((addr != null) && ((addr instanceof Inet4Address))) {
                    String ip = addr.getHostAddress();
                    list.add(ip);
                }
            }
        }
    
        public static void main(String[] args) {
            System.out.println(getLocalIP());
        }
    }
    
    
    展开全文
  • Java获取IP地址工具类

    2020-07-02 23:24:05
    package com.example.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; ... ...public class IPUtils { private static Logger logger = LoggerFactory.getLogger... * 获取IP地址 * * 使用N
  • java获取IP地址工具类

    2016-03-31 11:43:22
    利用淘宝IP地址库实现的利用IP获取用户所在地工具类
  • * 获取服务器ip地址 * */ public static String getServerIp() { // 获取操作系统类型 String sysType = System.getProperties().getProperty("os.name"); System.out.pri
  • java获取ip工具类

    2020-12-12 15:49:50
    java获取ip工具类 import com.alibaba.druid.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; /** * IP地址 */ public class ...
  • 1.获取主机对象InetAddress //获取本地主机对象 InetAddress host = InetAddress....//根据ip地址或主机名获取主机对象,以主机名获取主机时需要DNS解析 InetAddress host = InetAddress.getByName("192....
  • JAVA获取IP地址代码

    2014-12-29 10:44:00
    private static final String NUKNOWN = "unknown"; private static final String[] ADDR_HEADER = { "X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "X-Real-IP" };... * 获取客户端真实IP
  • java获取ip

    2019-09-19 10:59:48
    获取本地ip常用方法: InetAddress.getLocalHost().getHostAddress())容易得到 “127.0.0.1” ...可以获取本地所有的ip地址,如docker服务相关的ip等。 // often returns "127.0.0.1" System.out.println("Host...
  • 该工具类以读取本地纯真IP地址库实现,缺点不易更新 IPSeekerUtil.java 工具类 package com.gfan.yyq.yyqs.utils; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; ...
  • java 获取ip

    2016-01-07 21:27:23
    package com.qbao.bbs.util; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException;...import java.util.Enumeration; import javax.servlet.http.HttpServletRequest;
  • Java获取IP工具类

    2019-09-18 11:04:28
    import java.net.InetAddress; import java.net.NetworkInterface;...import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class NetworkUtil { public static String g...
  • java获取客户端IP地址

    2019-12-06 11:14:56
    创建IPHelper工具类 import javax.servlet.http.HttpServletRequest; public class IPHelper { public static String getIP(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-fo...
  • Java获取用户的IP地址

    2019-04-10 09:31:42
    package com.lianqiao.dache.util; import javax.servlet.... /** IP工具类,获取用户的IP地址 * @author 秋枫艳梦 * @date 2019-04-10 * */ public class IPUtil { /** 静态方法,获取IP地址 * @param...
  • if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { //这里主要是为了防止出现代理或其他原因而的不到真正的IP地址; ip = request.getHeader("Proxy-Client-IP"); } if (ip == ...
  • 修改nginx.conf配置,增加配置,...然后调用获取IP工具类 package com.shijie.box.util; import javax.servlet.http.HttpServletRequest; public class NewUtils { /** * 获取请求ip * @param request * @return
  • 这几天搞了用java获取ip地址,由于测试方法不对,一直没有成功,昨天终于想通了并不是方法不对,而是我的测试方法不对,下面这个方法,完全可以得到客户端的公网ip地址 但是在测试的时候注意:我用的是weblogic...
  • Java获取系统IP地址

    2015-08-05 12:11:58
    在一个项目中如果你想获取系统的ip地址那么可能许多同学在想那是多么容易,但是如果在多种操作系统貌似就不那么简单了,下面看看怎么获取系统的ip地址的。 package easyway.tbs.commons; import java.net....
  • java获取ip和域名

    2017-12-24 16:35:00
    IP测试类:InetAddress 测试: package ip; import java.net.InetAddress;...import java.net.UnknownHostException;...import java.util.Arrays; /** * java 中提供的两个类 * InetAddress 获取i...
  • 文章目录在Controller中获取用户请求把用户request请求传入Util工具类中获取IP地址引入maven通过IP地址获取地址信息工具类 在Controller中获取用户请求 public void header(Model model){ /** * 获取ip地址 */ ...
  • * 功能说明:获取DNS服务器 地址 * * 参数说明: * @return * */ public static String getDns() { String s = ""; try { String s1 = "ipconfig /all"; Process process = Runtime.getRuntime()....
  • java获取本地ip地址(Windows和Linux通用)

    千次阅读 2019-04-24 17:11:57
    package ... import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration;...* 获取本地ip地址的方法(Linux环境和Java通用) * @ver...
  • Java 获取请求主机IP地址

    千次阅读 2016-01-01 12:45:05
    本文向您介绍使用Java获取IP地址的方法,在request.getRemoteAddr()方法在使用方向代理失效的情况下,本文的方法依然可以获取真实地址。在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 65,701
精华内容 26,280
关键字:

java获取iputil

java 订阅