導航:首頁 > 編程語言 > java獲取用戶ip

java獲取用戶ip

發布時間:2023-12-04 15:40:45

1. java如何查詢本機ip地址和mac地址

Java中可以使用程序來獲取本地ip地址和mac地址,使用InetAddress這個工具類,示例如下:

importjava.net.*;
publicclassNetInfo{
publicstaticvoidmain(String[]args){
newNetInfo().say();
}
publicvoidsay(){
try{
InetAddressi=InetAddress.getLocalHost();
System.out.println(i);//計算機名稱和IP
System.out.println(i.getHostName());//名稱
System.out.println(i.getHostAddress());//只獲得IP
}
catch(Exceptione){e.printStackTrace();}
}
}

也可以通過命令行窗口來查看本地ip和mac地址,輸入命令:ipconfig。

2. java中獲取本地IP地址

方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmd.exe /c ipconfig | findstr IPv4";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
line = line.substring(line.lastIndexOf(":")+2,line.length());
sb.append(line);
}
br.close();
p.destroy();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
sb.append(inetAddress.getHostAddress().toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sb.toString();
}

3. java IP查詢方法

Java編程查詢IP地址歸屬地,可以調用淘寶提供的service查詢,並且解析http請求返回的json串,代碼如下:

packagegetAddressByIp;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importnet.sf.json.JSONObject;

publicclassGetAddressByIp
{
/**
*
*@paramIP
*@return
*/
(StringIP){
Stringresout="";
try{
Stringstr=getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+IP);
System.out.println(str);
JSONObjectobj=JSONObject.fromObject(str);
JSONObjectobj2=(JSONObject)obj.get("data");
Stringcode=(String)obj.get("code");
if(code.equals("0")){
resout=obj2.get("country")+"--"+obj2.get("area")+"--"+obj2.get("city")+"--"+obj2.get("isp");
}else{
resout="IP地址有誤";
}
}catch(Exceptione){

e.printStackTrace();
resout="獲取IP地址異常:"+e.getMessage();
}
returnresout;

}

(StringurlStr)
{
try
{//獲取HttpURLConnection連接對象
URLurl=newURL(urlStr);
HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();
//設置連接屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
//獲取相應碼
intrespCode=httpConn.getResponseCode();
if(respCode==200)
{
returnConvertStream2Json(httpConn.getInputStream());
}
}
catch(MalformedURLExceptione)
{
e.printStackTrace();
}
catch(IOExceptione)
{
e.printStackTrace();
}
return"";
}

(InputStreaminputStream)
{
StringjsonStr="";
//ByteArrayOutputStream相當於內存輸出流
ByteArrayOutputStreamout=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen=0;
//將輸入流轉移到內存輸出流中
try
{
while((len=inputStream.read(buffer,0,buffer.length))!=-1)
{
out.write(buffer,0,len);
}
//將內存流轉換為字元串
jsonStr=newString(out.toByteArray());
}
catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnjsonStr;
}
}

4. JAVA如何獲取客戶端IP地址和MAC地址

/**
* 獲取IP地址
* @return
*/
public static String GetNetIp() {
URL infoUrl = null;
InputStream inStream = null;
String line = "";
try {
infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inStream = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
// 從反饋的結果中提取出IP地址
int start = strber.indexOf("{");
int end = strber.indexOf("}");
String json = strber.substring(start, end + 1);
if (json != null) {
try {
JSONObject jsonObject = new JSONObject(json);
line = jsonObject.optString("cip");
} catch (JSONException e) {
e.printStackTrace();
}
}
return line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}

public static String getLocalMacAddress() {//沒有緩存的地址,則查詢
String mac_s = ""; try { byte[] mac;
NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
mac = ne.getHardwareAddress();
mac_s = byte2hex(mac);
} catch (Exception e) {
} mac_s; return mac_s;
}

閱讀全文

與java獲取用戶ip相關的資料

熱點內容
網站圖標素材壓縮包 瀏覽:890
娛樂化app怎麼做 瀏覽:636
加密貨幣行業前景如何 瀏覽:572
arm查詢法的局限性和編譯流程 瀏覽:78
醒圖的文件夾叫什麼 瀏覽:998
php程序員北京 瀏覽:175
gcc編譯進程數據 瀏覽:653
手機上的文件夾是怎樣的 瀏覽:166
微雲群共享文件夾改變 瀏覽:534
程序員三年後能做什麼 瀏覽:449
分解運演算法則 瀏覽:876
python腳本執行sudo 瀏覽:721
安徽科海壓縮機 瀏覽:372
怎麼下載app里的講義 瀏覽:158
命令重啟伺服器 瀏覽:210
android電視root許可權獲取 瀏覽:249
解放戰爭pdf王樹增 瀏覽:685
python壓測app介面 瀏覽:953
抖音app怎麼推薦 瀏覽:100
歌庫伺服器能做其他什麼用途 瀏覽:95