欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java根據(jù)本地IP獲取mac地址的方法

 更新時(shí)間:2017年06月29日 08:38:35   作者:java根據(jù)本地Ip獲取mac地址  
這篇文章主要為大家詳細(xì)介紹了java根據(jù)本地IP獲取mac地址的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java根據(jù)本地IP獲取mac地址的具體代碼,供大家參考,具體內(nèi)容如下

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MacAddress {

 /**
 * @param args
 * @throws UnknownHostException 
 * @throws SocketException 
 */
 public static void main(String[] args) throws UnknownHostException, SocketException {

 InetAddress ia = InetAddress.getLocalHost();
 System.out.println(ia);
 getLocalMac(ia);
 }
 private static void getLocalMac(InetAddress ia) throws SocketException {
 // TODO Auto-generated method stub
 //獲取網(wǎng)卡,獲取地址
 byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
 
 System.out.println("mac數(shù)組長(zhǎng)度:"+mac.length);
 StringBuffer sb = new StringBuffer("");
 for(int i=0; i<mac.length; i++) {
  if(i!=0) {
  sb.append("-");
  }
  //字節(jié)轉(zhuǎn)換為整數(shù)
  int temp = mac[i]&0xff;
  String str = Integer.toHexString(temp);
  System.out.println("每8位:"+str);
  if(str.length()==1) {
  sb.append("0"+str);
  }else {
  sb.append(str);
  }
 }
 System.out.println("本機(jī)MAC地址:"+sb.toString().toUpperCase());
 }
}

下面這個(gè)方法是獲取客戶端請(qǐng)求地址

public String getClientIp(HttpServletRequest request) {
 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();

 }
 if(ip.trim().contains(",")){
  String [] ips=ip.split(",");
  ip=ips[0];
 }
 return ip;
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論