SNMP接收中文问题

SNMP接受中文问题

收到含有中文的消息格式

7b:22:61:67:65:22:3a:31:38:2c:22:70:61:73:73:77:6f:72:64:22:3a:22:31:32:33:35:34:38:22:2c:22:75:73:65:72:6e:61:6d:65:22:3a:22:e5:bc:a0:e4:b8:89:22:7d

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 实现CommandResponder的processPdu方法
* 用于处理传入的请求、PDU等信息 当接收到trap时,会自动进入这个方法
*/
@Override
public void processPdu(CommandResponderEvent respEvnt) {
// 解析Response
if (respEvnt != null && respEvnt.getPDU() != null) {

Vector<VariableBinding> recVBs = (Vector<VariableBinding>) respEvnt.getPDU().getVariableBindings();
for (int i = 0; i < recVBs.size(); i++) {
VariableBinding recVB = recVBs.elementAt(i);
String message = getMessage(recVB);
System.out.println(recVB.getOid() + " : " + message);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private String getMessage(VariableBinding recVB) {
String message = recVB.getVariable().toString();

// 传过来的含有冒号,且不是JSON字符串
if (message.indexOf(":") != -1 && message.indexOf("{") != 0) {
String[] temps = message.split(":");
byte[] bytes = new byte[temps.length];
for (int j = 0; j < temps.length; j++) {
bytes[j] = (byte) Integer.parseInt(temps[j], 0x10);
}

try {
message = new String(bytes, "utf8");
} catch (UnsupportedEncodingException e) {
System.out.println("未知的字符编码!");
}
}
return message;
}