如何将输入流转换为Java对象

11
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(reg_be);
oos.flush();
oos.close();

InputStream is = new ByteArrayInputStream(baos.toByteArray());

这段代码是将Java中的Object转换为InputStream,我该如何将InputStream转换回Object?我需要将我的Object转换为InputStream,然后传递它并希望能够还原成Object


2
ObjectInputStream 看起来非常明显。 - Gimby
4个回答

17

try 块中,你应该编写:

ObjectInputStream ois = new ObjectInputStream(is);
Object object = ois.readObject();

ObjectInputStream 通过另一个流(如 BufferedInputStream 或输入流 is)进行初始化。


ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(reg_be); oos.flush(); oos.close(); InputStream is = new ByteArrayInputStream(baos.toByteArray()); 这段代码为什么会抛出 java.io.NotSerializableException 异常? - sabarirajan
因为你正在序列化/反序列化的对象类(reg_be)必须实现Serializable接口。 - darijan

1
ObjectInputStream ois = new ObjectInputStream(is);
Object object = ois.readObject();

ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(reg_be); oos.flush(); oos.close();InputStream is = new ByteArrayInputStream(baos.toByteArray()); 这段代码为什么会抛出异常 java.io.NotSerializableException:? - sabarirajan
因为你的类没有实现Serializable接口。 - Buhake Sindi

0
尝试以下内容
ObjectInputStream  ois = new ObjectInputStream(is);
Object obj = ois .readObject();

ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(reg_be); oos.flush(); oos.close(); InputStream is = new ByteArrayInputStream(baos.toByteArray()); 为什么这段代码会报出异常java.io.NotSerializableException? - sabarirajan
为什么要发布完全相同的答案,就像前面两个一样? - darijan
你的类实现了Serializable接口吗?如果没有,你将会得到那个异常。这个类的对象是你在I/O流中传输的。 - Aniket Thakur
@darijan 没有看到前面的两个。 - Aniket Thakur
好的,我需要序列化我的对象...好的,我会做的。谢谢你的帮助。 - sabarirajan

0
ObjectInputStream ois = new ObjectInputStream(is);
Object object = ois.readObject();

如@darijan所述,它运行良好。 但是我们需要为该代码添加try-catch块,并且对于空输入流,它将给出EOF(文件结束)相关错误。

因此,我将其转换为字符串。然后,如果字符串不为空或null,则仅使用ObjectMapper将其转换为对象

虽然这不是一种有效的方法,但我不需要担心try-catch,空处理也在字符串中完成而不是在输入流中

String responseStr = IOUtils.toString(is, StandardCharsets.UTF_8.name());
Object object = null;
// is not null or whitespace consisted string
if (StringUtils.isNotBlank(response)) {
    object = getJsonFromString(response);
}


// below codes are already used in project (Util classes)
private Object getJsonFromString(String jsonStr) {
    if (StringUtils.isEmpty(jsonStr)) {
        return new LinkedHashMap<>();
    }
    ObjectMapper objectMapper = getObjectMapper();

    Map<Object, Object> obj = null;
    try {
        obj = objectMapper.readValue(jsonStr, new TypeReference<Map<Object, Object>>() {
        });
    } catch (IOException e) {
        LOGGER.error("Unable to parse JSON : {}",e)
    }
    return obj;
}
private ObjectMapper getObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    return objectMapper;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接