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
} |