Java 从字节字符串转换对象时,流标头无效:EFBFBDEF

Java 从字节字符串转换对象时,流标头无效:EFBFBDEF,java,arrays,Java,Arrays,我正在尝试将ArrayList对象转换为字节字符串,以便通过套接字发送。当我运行此代码时,它会正确地转换为字符串,但当我尝试将其转换回时,会出现异常“java.io.StreamCorruptedException:invalid stream header:EFBFBDEF”。我在这里看到的其他答案并没有真正的帮助,因为我正在使用匹配的ObjectOutputStream和ObjectInputStream。很抱歉,如果有一个简单的修复,因为我是新的工作与流对象 try { A

我正在尝试将ArrayList对象转换为字节字符串,以便通过套接字发送。当我运行此代码时,它会正确地转换为字符串,但当我尝试将其转换回时,会出现异常“java.io.StreamCorruptedException:invalid stream header:EFBFBDEF”。我在这里看到的其他答案并没有真正的帮助,因为我正在使用匹配的ObjectOutputStream和ObjectInputStream。很抱歉,如果有一个简单的修复,因为我是新的工作与流对象

try {
        ArrayList<String> text = new ArrayList<>();
        text.add("Hello World!");
        String byteString = Utils.StringUtils.convertToByteString(text);
        ArrayList<String> convertedSet = (ArrayList<String>) Utils.StringUtils.convertFromByteString(byteString);
        VCS.getServiceManager().addConsoleLog(convertedSet.get(0));
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

public static String convertToByteString(Object object) throws IOException {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) {
            out.writeObject(object);
            final byte[] byteArray = bos.toByteArray();
            return new String(byteArray);
        }
    }

public static Object convertFromByteString(String byteString) throws IOException, ClassNotFoundException {
        final byte[] bytes = byteString.getBytes();
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)) {
            return in.readObject();
        }
    }
试试看{
ArrayList text=新的ArrayList();
添加(“你好,世界!”);
String byteString=Utils.StringUtils.convertToByteString(文本);
ArrayList convertedSet=(ArrayList)Utils.StringUtils.convertFromByteString(byteString);
getServiceManager().addConsoleLog(convertedSet.get(0));
}捕获(IOException | ClassNotFoundException e){
e、 printStackTrace();
}
公共静态字符串convertToByteString(对象对象)引发IOException{
try(ByteArrayOutputStream bos=newbytearrayoutputstream();ObjectOutput out=newobjectoutputstream(bos)){
out.writeObject(对象);
最后一个字节[]byteArray=bos.toByteArray();
返回新字符串(byteArray);
}
}
公共静态对象convertFromByteString(String byteString)引发IOException、ClassNotFoundException{
final byte[]bytes=byteString.getBytes();
try(ByteArrayInputStream bis=newbytearrayinputstream(字节);ObjectInput in=newobjectinputstream(bis)){
在.readObject()中返回;
}
}

我想出来了。我必须使用Base64编码。转换方法必须更改为以下内容:

public static String convertToByteString(Object object) throws IOException {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) {
            out.writeObject(object);
            final byte[] byteArray = bos.toByteArray();
            return Base64.getEncoder().encodeToString(byteArray);
        }
    }

public static Object convertFromByteString(String byteString) throws IOException, ClassNotFoundException {
        final byte[] bytes = Base64.getDecoder().decode(byteString);
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)) {
            return in.readObject();
        }
    }

字符串不是二进制数据的容器。您需要传递原始字节数组,或者对其进行十六进制或base64编码


更好的办法是,直接序列化到套接字,然后完全消除它。

在平台默认编码中,所有字节序列都不是有效的字符序列。不要将任意字节转换为字符串。只需使用字节数组。如果您确实需要一些可打印的内容,那么使用base64编码将字节数组转换为可打印字符串。阅读感谢的文章,这是一个很好的阅读,并帮助我了解更多!你不必使用base-64。有几个更简单的选择。你可以阅读我的answe,当你问我时,它已经在那里呆了三个月。