1.获取客户端ip地址( 这个必需年夜客户端传到后台): jsp页面下,很简单,request.getRemoteAddr() ;
8 |1 V: Q& A3 H# i9 W; T/ t' \ 因为系统的VIew层是用JSF来实现的,是以页面膳缦慊法直接获得近似request,在bean里做了个强制转换, H! A) u ^2 \
public String getMyIP() {3 a. L1 z5 }4 o* K1 v
try {6 i6 Q/ n0 _/ T' ^( Y/ s# l
FacesContext fc = FacesContext.getCurrentInstance();5 e3 S( C2 |# M0 u. P' h. o: ]1 K
HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
% @1 _ E# R* N! v+ K+ {) ^ return request.getRemoteAddr();
% Y7 M5 L4 z, {5 _3 ~ }
2 E; S3 G5 r5 y% H/ N catch (Exception e) {: d! F. i3 W" e& I' o+ H
e.printStackTrace();% c- c" ^+ Q W9 |: K4 @% T
}
8 W5 M% r6 w f$ {2 l% q5 I, E! U return "";; n, n+ U, X% \* T* p- Y
}- p5 K6 V9 {# m* K, w* }6 N" J; C
(测试经由过程)& U5 \8 Y1 _7 X1 N1 a- G3 A
2.获取客户端mac地址
7 w h+ R% {5 d7 G8 H6 x+ C 挪用window的呼吁,在后台Bean诚恳现 经由过程ip来获取mac地址。体例如下:& q! E7 w l! v4 k/ V
public String getMACAddress(String ip){
' g; L7 S. R M4 B0 w/ l String str = "";( O9 q; p# z2 d% \* E9 M
String macAddress = ""; |6 |! B" K; k% ]. `; {# e
try {
) z2 R. U; e' c9 n Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
7 s4 v: @, ~2 m# H; l/ R* s3 b InputStreamReader ir = new InputStreamReader(p.getInputStream());4 E) W/ P6 T/ S1 X9 K
LineNumberReader input = new LineNumberReader(ir);
+ v% ]/ Y' ]% I8 t for (int i = 1; i < 100; i++) {# A5 Q1 r- b, t% N8 R2 m
str = input.readLine();
& Q; k0 [7 z, {$ ] if (str != null) {8 H9 Z' y, C- Q7 j5 b- B8 a/ p/ N
if (str.indexOf("MAC Address") > 1) {
* T4 {5 Z! @) G0 i, i0 ^, _" w& h+ ` macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());* n, c2 v. ?- D: r2 b2 |. ~
break;
: q/ G$ n# T/ U( D } ]) o5 i4 e3 M* l( D
}
+ A8 h2 f3 g% {$ n$ j }& T v3 S/ s, j5 n8 @$ v
} catch (IOException e) {
) S& c8 `- j2 X; r) }' H& }! `6 h; y e.printStackTrace(System.out);
8 D1 Z. D) e |6 f1 K, O3 p }
1 O$ N [' N: P) C3 x! H return macAddress;5 k( A; c( v* w: ^/ I- w$ ^
} |