【java工具類】IPUtil

import java.io.BufferedReader;
import java.io.InputStreamReader;


import org.apache.struts2.ServletActionContext;

public class Iputil {
    public static String getIp(){
        String ip = ServletActionContext.getRequest().getRemoteAddr();
        return ip;
    }
    /**
     * 獲取網卡MAC地址
     */
    public static String getMacOnWindow() {
        try {
            String mac = null;
            Process process = Runtime.getRuntime().exec("ipconfig /all");
            BufferedReader buffer = new BufferedReader(new InputStreamReader(process.getInputStream()));
            for (String line = buffer.readLine(); line != null; line = buffer.readLine()) {
                int index = line.indexOf("Physical Address");
                    if (index <= 0) {
                        continue;
                    }
                mac = line.substring(index + 36);
                break;
            }
            buffer.close();
            process.waitFor();
            return mac;
        } catch (Exception exception) {
            return null;
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章