0%

SNMP4J 中文乱码问题解决

因为项目原因,用到了snmp trap,使用UDP 162端口来接收SNMP报警信息。
在显示中文和日期时会出现乱码,具体原因网上写的比较多,我就不做解释了。
从网上找了一部分代码,总有点问题,然后修修改改中文能显示了,但是遇到日期仍然有问题,然后又找了一部分代码,将其整合,终于解决了,先粘一下代码,后面如果改动了再作更新。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 处理SNMP事件

public void processPdu(CommandResponderEvent event) {
System.out.println("processPdu");

String version = null;
String community = null;
if (event.getPDU().getType() == PDU.V1TRAP) {
version = "v1";
community = new String(event.getSecurityName());
} else if (event.getPDU().getType() == PDU.TRAP) {
if (event.getSecurityModel() == 2) {
version = "v2";
community = new String(event.getSecurityName());
} else {
version = "v3";
}
}

PDU pdu = event.getPDU();

String output = "SNMP:[IP = " + event.getPeerAddress() + "]";
// System.out.println(output);

if (event != null && event.getPDU() != null) {
Vector<VariableBinding> recVBs = (Vector<VariableBinding>) pdu.getVariableBindings();
for (int i = 0; i < recVBs.size(); i++) {
VariableBinding recVB = recVBs.elementAt(i);

String des = OID.get(recVB.getOid().toString()) + " : " + getChinese(recVB.getVariable().toString());
// System.out.println(des);
output += "\n" + des;

// }
}
}

System.out.println(output);


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// octetString 转 二进制

private static int[] octetStringToBytes(String value_ipar) {
// ---------------------------
// Split string into its parts
// ---------------------------
String[] bytes;
bytes = value_ipar.split("[^0-9A-Fa-f]");

// -----------------
// Initialize result
// -----------------
int[] result;
result = new int[bytes.length];

// -------------
// Convert bytes
// -------------
int counter;
for (counter = 0; counter < bytes.length; counter++)
result[counter] = Integer.parseInt(bytes[counter], 16);

// ----
// Done
// ----
return (result);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// octetString 转 Date

private static Date octetStringToDate(String value_ipar) throws Exception {
// ---------------------------
// Convert into array of bytes
// ---------------------------
int[] bytes;
bytes = octetStringToBytes(value_ipar);

// -----------------------
// Maybe nothing specified
// -----------------------
if (bytes[0] == 0)
return (null);

// ------------------
// Extract parameters
// ------------------
int year;
int month;
int day;
int hour;
int minute;
int second;
int deci_sec = 0;
int offset = 0;
year = (bytes[0] * 256) + bytes[1];
month = bytes[2];
day = bytes[3];
hour = bytes[4];
minute = bytes[5];
second = bytes[6];
if (bytes.length >= 8)
deci_sec = bytes[7];
if (bytes.length >= 10) {
offset = bytes[9] * 60;
if (bytes.length >= 11)
offset += bytes[10];
if (bytes[8] == '-')
offset = -offset;
offset *= 60 * 1000;
}

// ------------------------------------
// Get current DST and time zone offset
// ------------------------------------
Calendar calendar;
int my_dst;
int my_zone;
calendar = Calendar.getInstance();
my_dst = calendar.get(Calendar.DST_OFFSET);
my_zone = calendar.get(Calendar.ZONE_OFFSET);

// ----------------------------------
// Compose result
// Month to be converted into 0-based
// ----------------------------------
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, deci_sec * 100);

// ---------
// Reset DST
// ---------
calendar.add(Calendar.MILLISECOND, my_dst);

// -----------------------------------------------------------------------------------
// If the offset is set, we have to convert the time using the offset of
// our time zone
// -----------------------------------------------------------------------------------
if (offset != 0) {
int delta;
delta = my_zone - offset;
calendar.add(Calendar.MILLISECOND, delta);
}

// -------------
// Return result
// -------------
return (calendar.getTime());

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// octetString 转 中文

private static String getChinese(String variable) {
String result = variable;

if (!variable.contains(":")) {
return result;
}

if (variable.length() == 23 || variable.length() == 32) {
try {
return format.format(octetStringToDate(variable));
} catch (Exception e) {
}
}

if (result.equals(variable)) {
try {

String[] temps = variable.split(":");
byte[] bs = new byte[temps.length];
for (int i = 0; i < temps.length; i++)
bs[i] = (byte) Integer.parseInt(temps[i], 16);
result = new String(bs, "utf-8");
} catch (Exception ex) {

}
}
return result;

}

参考网址:
http://www.cnblogs.com/yuanfy008/p/8046189.html
https://blog.csdn.net/appletreesujie/article/details/50752065
https://forum.huawei.com/enterprise/zh/thread-429027.html
http://www.mibdepot.com/cgi-bin/getmib3.cgi?win=mib_a&i=1&n=MSEA-MIB&r=johnsoncontrols&f=msea.mi2&v=v2&t=tree