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
|
@Override public void processPdu(CommandResponderEvent respEvnt) { 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();
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; }
|