導航:首頁 > 編程語言 > java獲得ip地址

java獲得ip地址

發布時間:2023-01-03 20:11:00

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。

⑵ java 怎麼獲取請求方ip地址

public
static
void
main(string[]
args)
{
try
{
//
獲取計算機名
string
name
=
inetaddress.getlocalhost().gethostname();
//
獲取ip地址
string
ip
=
inetaddress.getlocalhost().gethostaddress();
system.out.println("計算機名:"+name);
system.out.println("ip地址:"+ip);
}
catch
(unknownhostexception
e)
{
system.out.println("異常:"
+
e);
e.printstacktrace();
}
}
是否可以解決您的問題?

⑶ 如何用 Java 獲取系統 IP

importjava.net.*;

publicclassTest6{

publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
InetAddressia=null;
try{
ia=ia.getLocalHost();

Stringlocalname=ia.getHostName();
Stringlocalip=ia.getHostAddress();
System.out.println("本機名稱是:"+localname);
System.out.println("本機的ip是:"+localip);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

}

⑷ java怎麼獲取請求的ip

java獲取外網ip地址方法:
public class Main {

public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}

public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果沒有配置外網IP則返回它
String netip = null;// 外網IP

Enumeration<NetworkInterface> netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外網IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 外網IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 內網IP
localip = ip.getHostAddress();
}
}
}

if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}

⑸ java中如何獲取到本機的外網ip地址

java獲取本機的外網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;

/**
* 獲取本機外網IP地址
* 思想是訪問網站http://checkip.dyndns.org/,得到返回的文本後解析出本機在外網的IP地址
* @author pieryon
*
*/
public class ExternalIpAddressFetcher {
// 外網IP提供者的網址
private String externalIpProviderUrl;

// 本機外網IP地址
private String myExternalIpAddress;

public ExternalIpAddressFetcher(String externalIpProviderUrl) {
this.externalIpProviderUrl = externalIpProviderUrl;

String returnedhtml = fetchExternalIpProviderHTML(externalIpProviderUrl);

parse(returnedhtml);
}

/**
* 從外網提供者處獲得包含本機外網地址的字元串
* 從http://checkip.dyndns.org返回的字元串如下
* <html><head><title>Current IP Check</title></head><body>Current IP Address: 123.147.226.222</body></html>
* @param externalIpProviderUrl
* @return
*/
private String 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=new String(bytes,"UTF-8");

// 返回
return receivedString;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}

// 出現異常則返回空
return null;
}

/**
* 使用正則表達式解析返回的HTML文本,得到本機外網地址
* @param html
*/
private void parse(String html){
Pattern pattern=Pattern.compile("(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})", Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(html);
while(matcher.find()){
myExternalIpAddress=matcher.group(0);
}
}

/**
* 得到本機外網地址,得不到則為空
* @return
*/
public String getMyExternalIpAddress() {
return myExternalIpAddress;
}

public static void main(String[] args){
ExternalIpAddressFetcher fetcher=new ExternalIpAddressFetcher("http://checkip.dyndns.org/");

System.out.println(fetcher.getMyExternalIpAddress());
}
}

⑹ java如何獲取用戶真實的ip

1、如果伺服器如果沒有採用反向代理,而且客戶端沒有用正向代理的話,那麼可以獲取客戶端的真實IP地址request.getRemoteAddr()

2、如果伺服器如果沒有採用反向代理,而且客戶端有用正向代理的話,那麼通過request.getRemoteAddr()獲取客戶端的IP地址是客戶端 的代理伺服器的地址,並不是客戶端的真實地址,

3、如果客戶端使用的是多層代理的話,伺服器獲得的客戶端地址是客戶端的最外圍代理伺服器的地址如果伺服器如果採用反向代理伺服器,不管客戶端採用的是何種方式訪問伺服器。

//獲得客戶端真實IP地址的方法一:
publicStringgetRemortIP(HttpServletRequestrequest){
if(request.getHeader("x-forwarded-for")==null){
returnrequest.getRemoteAddr();
}
returnrequest.getHeader("x-forwarded-for");
}
//獲得客戶端真實IP地址的方法二:
publicStringgetIpAddr(HttpServletRequestrequest){
Stringip=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();
}
returnip;
}

⑺ java如何獲取當前時間,java如何獲取ip地址

publicstaticvoidmain(String[]args){

try{

System.out.println("當前時間:"+newDate());

System.out.println("IP地址:"+InetAddress.getLocalHost());

}catch(UnknownHostExceptione){

e.printStackTrace();

}

}

⑻ java怎麼通過域名獲取ip地址

importjava.net.InetAddress;
importjava.net.UnknownHostException;
publicclassTestInetAddress{
InetAddressmyIpAddress=null;
InetAddress[]myServer=null;
publicstaticvoidmain(Stringargs[]){
TestInetAddressaddress=newTestInetAddress();
System.out.println("YourhostIPis:"+address.getLocalhostIP());
Stringdomain=www.jb51.net;
System.out.println("Theserverdomainnameis:"+domain);
InetAddress[]array=address.getServerIP(domain);
intcount=0;
for(inti=1;i<array.length;i++){
System.out.println("ip"+i+""+address.getServerIP(domain)[i-1]);
count++;
}
System.out.println("IPaddresstotal:"+count);
}
/**
*獲得localhost的IP地址
*@return
*/
(){
try{
myIpAddress=InetAddress.getLocalHost();
}catch(UnknownHostExceptione){
e.printStackTrace();
}
return(myIpAddress);
}
/**
*獲得某域名的IP地址
*@paramdomain域名
*@return
*/
publicInetAddress[]getServerIP(Stringdomain){
try{
myServer=InetAddress.getAllByName(domain);
}catch(UnknownHostExceptione){
e.printStackTrace();
}
return(myServer);
}
}

閱讀全文

與java獲得ip地址相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:755
蘋果郵件無法連接伺服器地址 瀏覽:958
phpffmpeg轉碼 瀏覽:669
長沙好玩的解壓項目 瀏覽:140
專屬學情分析報告是什麼app 瀏覽:562
php工程部署 瀏覽:831
android全屏透明 瀏覽:730
阿里雲伺服器已開通怎麼辦 瀏覽:801
光遇為什麼登錄時伺服器已滿 瀏覽:300
PDF分析 瀏覽:482
h3c光纖全工半全工設置命令 瀏覽:139
公司法pdf下載 瀏覽:379
linuxmarkdown 瀏覽:349
華為手機怎麼多選文件夾 瀏覽:681
如何取消命令方塊指令 瀏覽:347
風翼app為什麼進不去了 瀏覽:776
im4java壓縮圖片 瀏覽:360
數據查詢網站源碼 瀏覽:148
伊克塞爾文檔怎麼進行加密 瀏覽:888
app轉賬是什麼 瀏覽:161