<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/java-get-the-ip-and-mac-address/</link>
<title><![CDATA[Java中如何获取Mac地址和IP地址]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Mon, 20 Sep 2010 18:07:55 +0000</pubDate> 
<guid>https://www.heckjj.com/java-get-the-ip-and-mac-address/</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 微软雅黑;"><span style="font-size: 14px;"><strong><span style="color: #4169E1;">获取本机的mac地址</span></strong></span><br/><br/>java代码中是无法访问硬件的，而JVM本身也不提供获取mac地址之类的方法。不过作为一个折中，你可以通过调用外部程序并截获输出来得到机器的硬件地址，下面是一段示例：</span><br/><textarea name="code" class="java" rows="15" cols="100">
package pkg;

import java.io.*;
/**
 * 获取MAC地址
 *
 */
public class GetMac &#123;
 // 通过IP获取网卡地址
 private String getMacAddressIP(String remotePcIP) &#123;
&nbsp;&nbsp;String str = &quot;&quot;;
&nbsp;&nbsp;String macAddress = &quot;&quot;;
&nbsp;&nbsp;try &#123;
&nbsp;&nbsp; Process pp = Runtime.getRuntime().exec(&quot;nbtstat -A &quot; + remotePcIP);
&nbsp;&nbsp; InputStreamReader ir = new InputStreamReader(pp.getInputStream());
&nbsp;&nbsp; LineNumberReader input = new LineNumberReader(ir);
&nbsp;&nbsp; for (int i = 1; i &lt; 100; i++) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;str = input.readLine();
&nbsp;&nbsp;&nbsp;&nbsp;if (str != null) &#123;
&nbsp;&nbsp;&nbsp;&nbsp; if (str.indexOf(&quot;MAC Address&quot;) &gt; 1) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;macAddress = str.substring(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.indexOf(&quot;MAC Address&quot;) + 14, str.length());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp; &#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp; &#125;
&nbsp;&nbsp;&#125; catch (IOException ex) &#123;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;return macAddress;
 &#125;

 // 通过机器名获取网卡地址
 private String getMacAddressName(String remotePcIP) &#123;
&nbsp;&nbsp;String str = &quot;&quot;;
&nbsp;&nbsp;String macAddress = &quot;&quot;;
&nbsp;&nbsp;try &#123;
&nbsp;&nbsp; Process pp = Runtime.getRuntime().exec(&quot;nbtstat -a &quot; + remotePcIP);
&nbsp;&nbsp; InputStreamReader ir = new InputStreamReader(pp.getInputStream());
&nbsp;&nbsp; LineNumberReader input = new LineNumberReader(ir);
&nbsp;&nbsp; for (int i = 1; i &lt; 100; i++) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;str = input.readLine();
&nbsp;&nbsp;&nbsp;&nbsp;if (str != null) &#123;
&nbsp;&nbsp;&nbsp;&nbsp; if (str.indexOf(&quot;MAC Address&quot;) &gt; 1) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;macAddress = str.substring(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str.indexOf(&quot;MAC Address&quot;) + 14, str.length());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp; &#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp; &#125;
&nbsp;&nbsp;&#125; catch (IOException ex) &#123;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;return macAddress;
 &#125;

 public static void main(String[] args) &#123;
&nbsp;&nbsp;GetMac getmac;
&nbsp;&nbsp;getmac = new GetMac();
&nbsp;&nbsp;String mac = &quot;&quot;;
&nbsp;&nbsp;mac = getmac.getMacAddressIP(&quot;192.168.0.100&quot;);// YOUR IP
&nbsp;&nbsp;System.out.println(mac);
&nbsp;&nbsp;mac = getmac.getMacAddressName(&quot;R61007&quot;);// YOUR HOST-NAME
&nbsp;&nbsp;System.out.println(mac);
 &#125;
&#125;
</textarea><br/><span style="font-family: 微软雅黑;"><span style="font-size: 14px;"><strong><span style="color: #4169E1;">获取本机的IP地址</span></strong></span><br/>下面代码示范了如何得到本机的IP地址：</span><br/><br/><textarea name="code" class="java" rows="15" cols="100">String ip = java.net.InetAddress.getLocalHost().getHostAddress()；</textarea><br/><span style="font-family: 微软雅黑;"><span style="font-size: 14px;"><strong><span style="color: #4169E1;">在JSP中获取访问客户端的IP地址以及主机名</span></strong></span><br/>下面代码示范了如何在JSP中得到访问该页面的客户端IP地址或者主机名：</span><br/><br/><textarea name="code" class="java" rows="15" cols="100">
&lt;%
&nbsp;&nbsp;String clientIP = request.getRemoteAddr();
&nbsp;&nbsp;String clientHostName = request.getRemoteHost();
&nbsp;&nbsp;String serverIP = java.net.InetAddress.getLocalHost().getHostAddress();
&nbsp;&nbsp;%&gt; 
</textarea><br/>Tags - <a href="https://www.heckjj.com/tags/java/" rel="tag">java</a> , <a href="https://www.heckjj.com/tags/%25E8%258E%25B7%25E5%258F%2596ip/" rel="tag">获取ip</a> , <a href="https://www.heckjj.com/tags/mac%25E5%259C%25B0%25E5%259D%2580/" rel="tag">mac地址</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/java-get-the-ip-and-mac-address/#blogcomment</link>
<title><![CDATA[[评论] Java中如何获取Mac地址和IP地址]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/java-get-the-ip-and-mac-address/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>