在我的代码中,我通过 Internet 发送一个编码为字节数组的 txt 文件,然后将消息转换回另一端并显示它。问题是当我尝试显示它时,它总是显示为“[B@1ef9f1d”或“[B@1764be1”等。
这是接收数据的内容
private void parsePacket(byte[] data, InetAddress address, int port) {
String datasent[] = (new String(data).trim()).split(",");
String type = datasent[0];
String message = datasent[1];
switch(type){//Data we are receiving from client, type is 5 char
default:
System.out.println(type);
System.out.println(message);
case "invalid":
println("Invalid packet", new Color(255, 155, 155));
break;
case "login":
addConnection(message, address, port);
break;
case "SendLog":
printLog(message);
break;
}
}
private void printLog(String message) {
int charperline = 10;
String line ="";
for (int i = 0; i < message.length() / charperline; i++){
for (int j = 0; j < charperline; j++){
line += message.charAt(i + j);
}
println("LOG: " + line);
line = "";
}
}
这就是发送它的原因:
public void sendLog(){
System.out.println("sendlog()");
InputStream is = getClass().getResourceAsStream("/LOG.txt");
try
{
byte[] text = new byte[10000];
is.read(text);
sendData(("SendLog," + text).getBytes());
//is.close();
new File("/LOG.txt").delete();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendData(byte[] data){
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 1332);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e){
e.printStackTrace();
}
}