使用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:\Windows\System32\drivers\etc\hosts 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