2011-11-15 46 views
4

我正在使用grails,我想查找會話用戶的公共IP,以便我可以找到位置國家/地區。Grails查找會話用戶的公共IP地址

我使用:

InetAddress.getLocalHost().getHostAddress() 

獲得IP,但問題是,它給我的私有IP即192.168.X.X這是沒有用的。

Thanx提前

+0

查看 - > http://stackoverflow.com/questions/2140859/how-do-you-get-client-ip-address-in - 一個grails控制器 –

+0

和http://stackoverflow.com/questions/7763338/how-to-find-out-connected-clients-ip-address-in-grails –

回答

6

你只需要使用

InetAdress.getHostAddress() 

但你可以只使用要求?

request.getRemoteAddr() 

在同一會話中,您還可以使用

String ipAddr = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()) 
     .getRequest().getRemoteAddr(); 
+1

Thanx的快速回復:)但沒有這些工作。只有request.getRemoteAddr()給出的值類似0:0:0:0:0:0:0:1,這是沒有用的。 – Wasim

+3

@Charsee你正在尋找一個在你的本地機器上運行的網站嗎?你通過本地主機連接。當從另一臺計算機上查看網站時,試着查看'request.remoteAddr'的輸出... –

+0

thanx tim和niels的幫助:)它現在在主服務器上工作 – Wasim