我在 jpcap 库的帮助下在 java 中的以太网(eth0)上嗅探数据包......所以,在我的项目中,我有一个 JpcapCaptor ......
//Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20);
captor.setFilter("icmp", true);
captor.loopPacket(-1, new PacketPrinter());
然后我有数据包打印机,它打印嗅探数据包的主体......
public class PacketPrinter implements PacketReceiver {
@Override
public void receivePacket(Packet packet) {
InputStream is = new ByteArrayInputStream(packet.data);
try {
String sstr = IOUtils.toString(is, "UTF-8");
System.out.println("STRING " + sstr);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String ss;
try {
ss = new String(packet.data, "UTF-8");
System.out.println("STRING " + ss);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但是有一个问题...... packet.data是一个字节[]......并且控制台将其打印为
STRING W�xQ�� !"#$%&'()*+,-./01234567
STRING W�xQ�� !"#$%&'()*+,-./01234567
STRING W�xQ�� !"#$%&'()*+,-./01234567
据我了解,这是因为编码问题???决定这个问题的解决方案是什么?