天天看點

jsp擷取用戶端ip和mac位址

jsp擷取用戶端ip和mac位址

對使用者的 ip 和 mac 位址進行驗證,這裡用到擷取用戶端ip和mac位址的兩個方法,留存。

1.擷取用戶端ip位址( 這個必須從用戶端傳到背景):

   jsp頁面下,很簡單,request.getRemoteAddr() ;

   因為系統的VIew層是用JSF來實作的,是以頁面上沒法直接獲得類似request,在bean裡做了個強制轉換

   public String getMyIP() {

Java代碼  

jsp擷取用戶端ip和mac位址
jsp擷取用戶端ip和mac位址

 try  { 

     FacesContext fc = FacesContext.getCurrentInstance(); 

     HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest(); 

     return  request.getRemoteAddr(); 

 } 

 catch  (Exception e) { 

     e.printStackTrace(); 

 } 

 return   "" ;

2.擷取用戶端mac位址

    調用window的指令,在背景Bean裡實作 通過ip來擷取mac位址。方法如下:

    public String getMACAddress(String ip){

Java代碼  

jsp擷取用戶端ip和mac位址

 String str =  "" ; 

 String macAddress = "" ; 

 try  { 

     Process p = Runtime.getRuntime().exec("nbtstat -A "  + ip); 

     InputStreamReader ir = new  InputStreamReader(p.getInputStream()); 

     LineNumberReader input = new  LineNumberReader(ir); 

     for  ( int  i =  1 ; i <  100 ; i++) { 

         str = input.readLine(); 

         if  (str !=  null ) { 

             if  (str.indexOf( "MAC Address" ) >  1 ) { 

                 macAddress = str.substring(str.indexOf("MAC Address" ) +  14 , str.length()); 

                 break ; 

             } 

         } 

     } 

 } catch  (IOException e) { 

     e.printStackTrace(System.out); 

 } 

 return  macAddress;

jsp擷取用戶端ip和mac位址