a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 133|回复: 1

[其他] JAVA技巧:获取客户端ipmac地址完整代码详解

[复制链接]
发表于 2012-8-4 12:28:23 | 显示全部楼层 |阅读模式
1.获取客户端ip地址( 这个必需年夜客户端传到后台):   jsp页面下,很简单,request.getRemoteAddr() ;
: |+ W3 }; O4 H) c/ C5 U  因为系统的VIew层是用JSF来实现的,是以页面膳缦慊法直接获得近似request,在bean里做了个强制转换
) U, i& Q1 U0 s( q  x9 Y  public String getMyIP() {
1 r. g' f4 J8 c/ T: L  try {
" k1 m8 c/ O, g, t! `& ]0 o: W& P  FacesContext fc = FacesContext.getCurrentInstance();! r5 g6 R) D& T
  HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
' V* m4 t1 m) o, o5 F  return request.getRemoteAddr();; `+ ~. M" }) O$ s0 o0 w
  }. }. T9 B8 C9 |
  catch (Exception e) {
4 D3 a1 E+ F$ E* Y1 e. c  e.printStackTrace();! F; N; h3 z. Q& {7 c7 X' V
  }
& M, ?) J" y" x* D( d: ?  return "";
$ i$ c% Z0 A% S& h, _. g  }5 a: y1 q2 |) k
  (测试经由过程)
* \: n  e; Q6 Y  2.获取客户端mac地址# z3 Y" L3 @- E8 ^" g
  挪用window的呼吁,在后台Bean诚恳现 经由过程ip来获取mac地址。体例如下:, P" N* g6 s2 ?) y
  public String getMACAddress(String ip){
- G0 V0 W* u" V% H/ N  String str = "";8 c1 S$ _) k5 p+ t3 {$ W
  String macAddress = "";
( ^- e: x% _5 f# E* f- Y  try {2 f7 B/ I5 [/ T7 a6 ?" d
  Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);3 W6 r3 }9 w7 b/ }
 InputStreamReader ir = new InputStreamReader(p.getInputStream());" q* r' d) z$ V, f: t& {
. @* R7 o* B& T" _6 _% W2 k- p
  LineNumberReader input = new LineNumberReader(ir);
回复

使用道具 举报

 楼主| 发表于 2012-8-4 12:28:24 | 显示全部楼层

JAVA技巧:获取客户端ipmac地址完整代码详解

</p>  for (int i = 1; i < 100; i++) {
) g- |3 C, D; i/ s  str = input.readLine();
" f( F: v! D3 l7 u, w5 X1 v" t  if (str != null) {
, q; S; Q4 C9 S3 x' x  if (str.indexOf("MAC Address") > 1) {
# K# S* y/ F% l0 t- a* K; P) j  macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());+ n1 O; Q. z8 C3 {  ]
  break;4 z: r0 E$ W% s6 u+ f( T. m
  }2 }& t4 ]1 C) z1 E6 c: f* f# A
  }
1 B. q0 ~2 l( D  {+ w# `  }
; M* K( Y* \) R& T  } catch (IOException e) {
  x7 P' G+ q4 c; h" l. G/ B  e.printStackTrace(System.out);: `: S+ V, B4 a5 ^1 L5 C3 p
  }
( h' K( G# H2 S) E  return macAddress;
( _* b* C9 E& b6 s  }
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-21 20:56 , Processed in 0.163667 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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