轉載:http://blog.csdn.net/ye1992/article/details/9492809
這幾天搞了用java獲取ip地址,由於測試方法不對,一直沒有成功,昨天終於想通了並不是方法不對,而是我的測試方法不對,下面這個方法,完全可以得到客戶端的公網ip地址
但是在測試的時候注意:我用的是weblogic測試的,在我的本機上開啓服務,然後訪問,由於同在一個局域網,因此下面這個方法返回的ip地址是127.0.0.1,而同在一個局域網的機器訪問的時候返回的ip是這臺機器的ip地址192.168.1.151,而外網的客戶端訪問的時候(這裏不是說測試服務器可以被外網訪問到),返回的ip是外網客戶端的公網ip地址
public String getIpAddr(HttpServletRequestrequest) {
String ip = request.getHeader("x-forwarded-for");
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();
}
return ip;
}
對於在jsp得到客戶端的ip地址有下面的方法
1.<input type="hidden" id="jiqiIP" name="jiqiIP"value="<%=request.getRemoteAddr()%>"/>
同樣分情況,本機訪問的時候,value是127.0.0.1同在局域網裏的其他機器的value是他們機器的ip,外網的也是他們機器的ip,不是客戶端公網ip地址,這個沒法得到客戶端公網的ip
2.<script language="JavaScript" src="http://www.hashemian.com/js/visitorIP.js.php"></script>這一段,可以得到客戶端的公網ip地址,只是我沒有用這個方法得公網ip我用的是上面的java方法
3.<iframe src="http://www.ip138.com/ip2city.asp"id="frame1"></iframe>用這個iframe也可以得到客戶端公網ip,但是IE卻很難得到iframe裏面的值,不論是控制iframe還是用ajax訪問那個url路徑都不能得到,因爲IE會報錯說拒絕訪問,最主要的原因是你跨域了(從網上找到的:如果可以跨的話,你不是可以隨便修改別人網站的內容了嗎?比如你在iframe里加載了百度,你又能通過js控制它,這不是爲黑客提供了便利的途徑嗎),因此無法得到ip,當你不跨域的時候是可以的,如下
myiframe.contentWindow.document.body.innerHTML方式直接通過iframe的id引用,只有IE系列瀏覽器支持,在其它標準瀏覽器中無效。
火狐還支持這種方法得到iframe的值,但是IE不支持contentDocument
<html>
<head>
<script type="text/javascript">
function getText(){
varx=document.getElementByIdx_x_x_x_x("frame1").contentDocument;
alert(x.getElementsByTagName_r("h3")[0].childNodes[0].nodeValue);
}
</script>
</head>
<body>
<iframe src="http://www.ip138.com/ip2city.asp"id="frame1"></iframe>
<br /><br/>
<input type="button" οnclick="getText()" value="Gettext" />
</body>
</html>
下面還有一個類可以得到服務器所在的公網ip
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExternalIpAddressFetcher {
//外網IP提供者的網址
privateString externalIpProviderUrl;
//本機外網IP地址
privateString myExternalIpAddress;
publicExternalIpAddressFetcher(String externalIpProviderUrl) {
this.externalIpProviderUrl = externalIpProviderUrl;
String returnedhtml =fetchExternalIpProviderHTML(externalIpProviderUrl);
parse(returnedhtml);
}
privateString fetchExternalIpProviderHTML(String externalIpProviderUrl){
// 輸入流
InputStream in = null;
// 到外網提供者的Http連接
HttpURLConnection httpConn = null;
try {
// 打開連接
URL url = new URL(externalIpProviderUrl);
httpConn = (HttpURLConnection)url.openConnection();
// 連接設置
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
// 獲取連接的輸入流
in = httpConn.getInputStream();
byte[] bytes=new byte[1024];// 此大小可根據實際情況調整
// 讀取到數組中
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=in.read(bytes, offset,bytes.length-offset)) >= 0) {
offset += numRead;
}
//將字節轉化爲爲UTF-8的字符串
String receivedString=newString(bytes,"UTF-8");
// 返回
return receivedString;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();