Java obtains the Client ip address mac address

Source: Internet
Author: User

1. obtain the Client IP address (this must be uploaded from the client to the backend): Under the jsp page, it is very simple, request. getRemoteAddr (); because the VIew layer of the system is implemented using JSF, the page cannot directly obtain similar requests, A forced conversion [java] public String getMyIP () {try {FacesContext fc = FacesContext is made in bean. getCurrentInstance (); HttpServletRequest request = (HttpServletRequest) fc. getExternalContext (). getRequest (); return request. getRemoteAddr ();} catch (Exception e) {e. printStackTrace ();} return "";} 2. obtain the client mac address Call the window command to obtain the mac address through the ip address in the backend Bean. The method is as follows: [java] public String getMACAddress (String ip) {String str = ""; String macAddress = ""; try {Process p = runtime.getruntime(cmd.exe c ("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;} added: Recently, I learned a lesson about how to obtain an IP address in linux. If you simply use InetAddress to obtain an IP address, the IP address is different on different machines. InetAddress. getLocalHost (). getAddress () actually obtains the IP address based on the hostname. In linux, the default hostname is localhost. Therefore, the local ip address obtained through the code above is 127.0.0.1. For example, my hostname is the ip address returned by linlin.atsig.com. It is indeed the address of atsig.com. Currently, the following code is not flexible enough: [java] public static byte [] getIp () throws UnknownHostException {byte [] B = InetAddress. getLocalHost (). getAddress (); Enumeration allNetInterfaces = null; try {allNetInterfaces = NetworkInterface. getNetworkInterfaces ();} catch (SocketException e) {e. printStackTrace ();} InetAddress ip = null; NetworkInterface netInterface = null; while (allNetInterfaces. hasMoreElements () {n EtInterface = (NetworkInterface) allNetInterfaces. nextElement (); if (netInterface. getName (). trim (). equals ("eth0") {Enumeration addresses = netInterface. getInetAddresses (); while (addresses. hasMoreElements () {ip = (InetAddress) addresses. nextElement () ;}break ;}} if (ip! = Null & ip instanceof Inet4Address) {return B = ip. getAddress ();} return B ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.