a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 132|回复: 0

[专业语言] JAVA认证:获取客户端ipmac地址完整代码详解

[复制链接]
发表于 2012-8-4 12:44:44 | 显示全部楼层 |阅读模式
1.获取客户端ip地址( 这个必需年夜客户端传到后台):   jsp页面下,很简单,request.getRemoteAddr() ;
9 \. I  n% N6 S  因为系统的VIew层是用JSF来实现的,是以页面膳缦慊法直接获得近似request,在bean里做了个强制转换
- S! J) x' `. t  public String getMyIP() {
. g4 N* ^8 O' D; h- N. @  try {& @5 h. @- i1 V/ D# L
  FacesContext fc = FacesContext.getCurrentInstance();- V, \- m+ Q& \, D( Q5 `* M; J
  HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();1 o1 d4 R0 K1 B) q; c* ~" r. c6 i1 g
  return request.getRemoteAddr();
7 ~' z) o) t$ j1 a! |, r( k) O7 S* k' l  }
) ?& C3 c, _! e6 J4 p  catch (Exception e) {0 p& c: B, d" _2 S! `: d
  e.printStackTrace();
: C' F6 `& x' i  }2 ]. U/ r7 i/ e" X
  return "";
. o( e  E# {; X3 [- j& U+ X  }- B( Y7 u! s: a, s( V
  (测试经由过程)7 M# k3 g  X2 w2 o
  2.获取客户端mac地址; c3 K  T% z: k, b- U2 v
  挪用window的呼吁,在后台Bean诚恳现 经由过程ip来获取mac地址。体例如下:
0 B1 z% @  P3 `/ Y0 U0 V  O$ q  public String getMACAddress(String ip){
  e" s& F2 M! Z0 Z; z8 P' N' Z  String str = "";
1 F8 p9 Z2 X% a  String macAddress = "";
3 t# X* G/ T3 W- o  try {" u% c# f, m: J$ Y6 @( p1 z
  Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
% @8 e! ~1 j" M0 ^/ r! m$ V6 B InputStreamReader ir = new InputStreamReader(p.getInputStream());9 K( L. W  t+ ~/ W/ _8 T* Q# v
  LineNumberReader input = new LineNumberReader(ir);
7 l6 ], v: |! q+ `' a) b$ U3 v4 ^  o  for (int i = 1; i < 100; i++) {
. P. \$ a" H6 T5 H1 i7 f6 b  str = input.readLine();
- l& j5 ^( I" V% f0 {  if (str != null) {
" ?7 s/ J! G/ j, h( @- h  if (str.indexOf("MAC Address") > 1) {/ f# \9 A6 b1 h" i1 p8 n0 e
  macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
, [8 g& L( g" Z% s8 {  p  break;) n" P: S- l4 E+ G  |8 K8 X
  }
7 [! {' R0 J7 N3 \2 c( S  `$ H  }! x9 m0 m& }, ]' ~- i& F: Q7 `% S
  }
/ K( z4 m* |8 ]( m  j8 T$ i4 d. ?  } catch (IOException e) {' f; V& B/ I. {9 ?- z
  e.printStackTrace(System.out);9 a6 i1 q  W9 d  }( q( G
  }* L+ l& @& ?5 _' B$ k
  return macAddress;: H' L8 ]' X$ n5 r6 W8 Q
  }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-7 09:54 , Processed in 0.205470 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表