java获取ip地址和Mac地址
2015-12-31 09:45
543 查看
public class GetMacAddress extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String ip = getRemoteAddress(request);
System.out.println(ip);
System.out.println(getMACAddress(ip));
}
public String getRemoteAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { //这里主要是为了防止出现代理或其他原因而的不到真正的IP地址;
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getRemoteAddr();
}
return ip;
}
public String getMACAddress(String ip) {
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); //相当于执行cdm命令
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;
}
}
标签: java物理地址 java网卡地址 定位电脑 杂谈分类: 程序之家
在JAVA中有时候会需要定位到某台电脑,而通过IP定位显然是不行的,有一种解决方案是通过定位到电脑的物理地址来定位电脑,这是本文要介绍的内容。 1. PhysicalAddressUtilpackage com.jetsum.web.util;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;import test.Test;
public class PhysicalAddress
{
public static String getMACAddress()
{ String address = "";
String os = System.getProperty("os.name");
if (os != null)
{
if (os.startsWith("Windows"))
{
try
{
ProcessBuilder pb = new ProcessBuilder("ipconfig", "/all");
Process p = pb.start();
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
if (line.indexOf("Physical Address") != -1)
{
int index = line.indexOf(":");
address = line.substring(index + 1);
break;
}
}
br.close();
return address.trim();
} catch (IOException e)
{ }
} else if (os.startsWith("Linux"))
{
try
{
ProcessBuilder pb = new ProcessBuilder("ifconfig");
Process p = pb.start();
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
int index = line.indexOf("硬件地址");
if (index != -1)
{
address = line.substring(index + 4);
break;
}
}
br.close();
return address.trim();
} catch (IOException ex)
{
Logger.getLogger(Test.class.getName()).log(Level.SEVERE,
null, ex);
} }
}
return address;
}
}
2.使用String address = PhysicalAddressUtil.getMACAddress() ;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String ip = getRemoteAddress(request);
System.out.println(ip);
System.out.println(getMACAddress(ip));
}
public String getRemoteAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { //这里主要是为了防止出现代理或其他原因而的不到真正的IP地址;
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getRemoteAddr();
}
return ip;
}
public String getMACAddress(String ip) {
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip); //相当于执行cdm命令
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;
}
}
标签: java物理地址 java网卡地址 定位电脑 杂谈分类: 程序之家
在JAVA中有时候会需要定位到某台电脑,而通过IP定位显然是不行的,有一种解决方案是通过定位到电脑的物理地址来定位电脑,这是本文要介绍的内容。 1. PhysicalAddressUtilpackage com.jetsum.web.util;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;import test.Test;
public class PhysicalAddress
{
public static String getMACAddress()
{ String address = "";
String os = System.getProperty("os.name");
if (os != null)
{
if (os.startsWith("Windows"))
{
try
{
ProcessBuilder pb = new ProcessBuilder("ipconfig", "/all");
Process p = pb.start();
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
if (line.indexOf("Physical Address") != -1)
{
int index = line.indexOf(":");
address = line.substring(index + 1);
break;
}
}
br.close();
return address.trim();
} catch (IOException e)
{ }
} else if (os.startsWith("Linux"))
{
try
{
ProcessBuilder pb = new ProcessBuilder("ifconfig");
Process p = pb.start();
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
int index = line.indexOf("硬件地址");
if (index != -1)
{
address = line.substring(index + 4);
break;
}
}
br.close();
return address.trim();
} catch (IOException ex)
{
Logger.getLogger(Test.class.getName()).log(Level.SEVERE,
null, ex);
} }
}
return address;
}
}
2.使用String address = PhysicalAddressUtil.getMACAddress() ;
相关文章推荐
- Eclipse版本控制插件Subclipse与Subversive的区别与选择
- 转,excel批量处理_java
- EXT Java 后台获取数据生成饼状图
- 运行测试Caused by: java.lang.UnsatisfiedLinkError: no attach in java.library.path错误解决
- eclipse中将Maven Dependencies Libraries移除后的恢复办法
- Java教程--接口篇
- Spring MVC 引文开发备忘录
- 解析Java中的Field类和Method类
- Mybits调用存储过程和函数返回游标,parameterType值必须为java.util.Map,由于它会把返回的游标放在里面
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- MyEclipse10.0 采用插件方式安装 SVN
- Eclipse启动遇到Error when loading the SDK
- Java并发和多线程4:使用通用同步工具CountDownLatch实现线程等待
- Java并发和多线程4:使用通用同步工具CountDownLatch实现线程等待
- Java并发和多线程3:线程调度和有条件取消调度
- Java并发和多线程3:线程调度和有条件取消调度
- Java并发和多线程3:线程调度和有条件取消调度
- java 全角引起的报错1
- [javase学习笔记]-7.11 main函数的解析与细节
- JAVA字符串格式化-String.format()的使用