java获取IP为 0:0:0:0:0:0:0:1 解决办法

使用request.getRemoteAddr()方法获取的值为0:0:0:0:0:0:0:1,一般来说如果不是自己的ip的话应该就是127.0.0.1

原因

​ 0:0:0:0:0:0:0:1是属于ipv6,但是本机又没有设置ipv6,后来我又进行另一台电脑做测试,发现这种情况只有在服务器和客户端都在同一台电脑上才会出现(例如用localhost访问的时候才会出现),原来是hosts配置文件的问题 windows的hosts文件路径:C:WindowsSystem32driversetchosts linux的host路径:/etc/hosts

解决措施

注释掉文件中的 # ::1 localhost 这一行即可解决问题。不过不起作用。

最有效的方式就是改变请求的ip,不要使用localhost:8080 使用127.0.0.1:8080或者ip:8080。百分百管用

或者这样

 if(request.getLocalAddr().toString().contains( "0:0:0:0:0:0:0:1")) //服务端和客户端在一台机器{ 
 	fileUrl=request.getScheme()+ "://127.0.0.1:"+request.getLocalPort()+ "/"+fullname ;
 } else { 
	fileUrl = request.getScheme() + "://" + request.getLocalAddr() + ":" + request.getLocalPort() + "/" + fullname ;
 }

转载自:https://blog.csdn.net/u010919083/article/details/79907821

或许是不知梦的缘故 流离之人追逐幻影
原文地址:https://www.cnblogs.com/cheng-/p/14297033.html