亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取遠程IP地址如180.171.121.32

如何獲取遠程IP地址如180.171.121.32

car 2017-10-29 12:45:55
????private?static?String?getRemoteIp(HttpServletRequest?request)?{ ????????//x-forwarded-for:代表客戶端,也就是HTTP的請求端真實的IP,只有在通過了HTTP代理或者負載均衡服務器時才會添加該項 ????????String?ip?=?request.getHeader("x-forwarded-for"); ????????//Proxy-Client-IP和WL-Proxy-Client-IP:?只有在Apache(Weblogic?Plug-In?Enable)+WebLogic搭配下出現,其中“WL”就是WebLogic的縮寫 ????????//訪問路徑是:Client?->?Apache?WebServer?+?Weblogic?http?plugin?->?Weblogic?Instances ????????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(); ????????} //????????ip?=?"61.51.253.90"; //????????ip?=?"218.25.19.153"; ????????//0:0:0:0:0:0:0:1:?IPV6的形式,win7下可能會出現 ????????//獲取遠程IP地址 ? ????????return?"0:0:0:0:0:0:0:1".equals(ip)???"127.0.0.1"?:?ip; ????}這段代碼只能返回0:0:0:0:0:0:0:1
查看完整描述

2 回答

已采納
?
逆光之羽

TA貢獻55條經驗 獲得超13個贊


ip是環回地址不是因為項目是在本機啟動,然后又是本機去調用對應接口造成的么?

查看完整回答
反對 回復 2017-10-30
  • car
    car
    意思是項目要在遠程啟動,才會真正的獲得正確IP地址
  • 逆光之羽
    逆光之羽
    其實我也沒試過,感覺應該是這個問題,代碼本身好像沒問題,主要是沒地方掛項目上去的話不好測試...
?
言曌博客liuyanzhao_com

TA貢獻164條經驗 獲得超117個贊

//獲得物理ip
public?static?String?getIpAddr(HttpServletRequest?request){
????String?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")?||?ipAddress.equals("0:0:0:0:0: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;
}


查看完整回答
反對 回復 2017-10-30
  • 2 回答
  • 0 關注
  • 1902 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號