Servelet 获取IP和MAC地址
packagecom.babyDuncan.java;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.LineNumberReader;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassipServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringip=getRemoteAddress(request);
System.out.println(ip);
System.out.println(getMACAddress(ip));
response.setContentType("text/html");
PrintWriterout=response.getWriter();
out
.println("<!DOCTYPEHTMLPUBLIC\"-//W3C//DTDHTML4.01Transitional//EN\">");
out.println("<HTML>");
out.println("<HEAD><TITLE>AServlet</TITLE></HEAD>");
out.println("<BODY>");
out.print("Thisis");
out.print(this.getClass());
out.println(",usingthePOSTmethod");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
publicStringgetRemoteAddress(HttpServletRequestrequest){
Stringip=request.getHeader("x-forwarded-for");
if(ip==null||ip.length()==0||ip.equalsIgnoreCase("unknown")){
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();
}
returnip;
}
publicStringgetMACAddress(Stringip){
Stringstr="";
StringmacAddress="";
try{
Processp=Runtime.getRuntime().exec("nbtstat-A"+ip);
InputStreamReaderir=newInputStreamReader(p.getInputStream());
LineNumberReaderinput=newLineNumberReader(ir);
for(inti=1;i<100;i++){
str=input.readLine();
if(str!=null){
if(str.indexOf("MACAddress")>1){
macAddress=str.substring(
str.indexOf("MACAddress")+14,str.length());
break;
}
}
}
}catch(IOExceptione){
e.printStackTrace(System.out);
}
returnmacAddress;
}
}