package com.dyt.apv.common.utils;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
/**
* 获取本机IP工具类
*
* Copyright: Copyright (c)2016
* Company: hxyd
* @author: hetiao
* @version: 1.0
* Create at: 2016年2月1日下午9:23:04
* @tags
*/
public class NetworkUtils {
public static List<String> getLocalIp(){
List<String> ipList = new ArrayList<String>();
try{
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
for (InterfaceAddress address : ni.getInterfaceAddresses()) {
if (address.getAddress() instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address.getAddress();
ipList.add(inet4Address.getHostAddress());
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return ipList;
}
/**
*
* 功能描述:获取真实的IP地址
* @param request
* @return
* @author guoyx
*/
public static String getIpAddr(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.getRemoteAddr();
}
if (!StringUtils.isBlank(ip) && ip.contains(","))
{
String[] ips = ip.split(",");
ip = ips[ips.length - 1];
}
ip = ip.replace(" ","");
//转换IP 格式
//if(!StringUtils.isBlank(ip)){
//ip=ip.replace(".", "_");
//}
return ip;
}
}
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
/**
* 获取本机IP工具类
*
* Copyright: Copyright (c)2016
* Company: hxyd
* @author: hetiao
* @version: 1.0
* Create at: 2016年2月1日下午9:23:04
* @tags
*/
public class NetworkUtils {
public static List<String> getLocalIp(){
List<String> ipList = new ArrayList<String>();
try{
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
for (InterfaceAddress address : ni.getInterfaceAddresses()) {
if (address.getAddress() instanceof Inet4Address) {
Inet4Address inet4Address = (Inet4Address) address.getAddress();
ipList.add(inet4Address.getHostAddress());
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return ipList;
}
/**
*
* 功能描述:获取真实的IP地址
* @param request
* @return
* @author guoyx
*/
public static String getIpAddr(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.getRemoteAddr();
}
if (!StringUtils.isBlank(ip) && ip.contains(","))
{
String[] ips = ip.split(",");
ip = ips[ips.length - 1];
}
ip = ip.replace(" ","");
//转换IP 格式
//if(!StringUtils.isBlank(ip)){
//ip=ip.replace(".", "_");
//}
return ip;
}
}