/**
* 获取外网IP
* @param request
* @return
*/
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null
// ipAddress = this.getRequest().getRemoteAddr()
ipAddress = request.getHeader("x-forwarded-for")
if (ipAddress == null || ipAddress.length() == 0
|| "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP")
}
if (ipAddress == null || ipAddress.length() == 0
|| "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP")
}
if (ipAddress == null || ipAddress.length() == 0
|| "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr()
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null
try {
inet = InetAddress.getLocalHost()
} catch (UnknownHostException e) {
e.printStackTrace()
}
ipAddress = inet.getHostAddress()
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null &&ipAddress.length() >15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") >0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","))
}
}
return ipAddress
}
————————————————
版权声明:本文为CSDN博主「秋9」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jlq_diligence/article/details/87871484
首先IP为一个字符串,例如:class test{
static void Split(string ip,out string str1)
{
int i=ip.length
while(i>0)
{
char ch=ip[i-1]
if(ch==':')
break
i--
}
str1=ip.Substring(0,i)
}
static void Main()
{
string str1
Split("192.168.0.255:8080",out str1)
Console.WriteLine("{0}",str1)
}
}
str1中保存的就是你的ip,192.168.0.255
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)