无效的流头:尝试从数据库中的序列化对象获取数据时出现 EFBFBDEF

问题描述 投票:0回答:1

我需要从此表中获取序列化对象(它不是文件或图像。只是数据库中包含列的表,其中存储了带有序列化数据的客户端),但是当我尝试读取对象时,我在线程“main”中出现异常“ java.io.StreamCorruptedException:无效的流头:EFBFBDEF。我知道当我们有ObjectInputStream时,我们需要有ObjectOutputStream。但我的表中有数据,不知道如何正确反序列化它。

这是我的班级

@Component

public class ClassicClientsService implements ClientDetailsService {

    private final JdbcTemplate dataTemplate;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public ClassicClientsService(DataSource dataSource) {
        this.dataTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws OAuth2Exception {

        try {
            ClientDetails details = dataTemplate.queryForObject("SELECT SERIALIZATION FROM OAUTH_CLIENTS WHERE CLIENTID = ?", new ClientDetailsMapper(), new Object[]{clientId});
            return details;
        } catch (EmptyResultDataAccessException ers) {
            throw new EmptyResultDataAccessException("Client " + clientId + " was not found", 1);
        }

    }

    private class ClientDetailsMapper implements RowMapper<ClientDetails> {

        @SneakyThrows
        @Override
        public ClientDetails mapRow(ResultSet rs, int rowNum) {
            byte[] temp = rs.getBytes("SERIALIZATION");
            
            return (ClientDetails) new ObjectInputStream(new ByteArrayInputStream(temp)).readObject();
        }

    }}

public boolean addClient(ClientDetails details) throws DataAccessException {
        ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
        try {
            ObjectOutputStream outObject = new ObjectOutputStream(outBytes);
            outObject.writeObject(details);
            outObject.flush();
            outObject.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] szDetails = outBytes.toByteArray();
        dataTemplate.update("INSERT INTO OAUTH_CLIENTS (CLIENTID, PROTOCOL, SERIALIZATION) VALUES(?,1,?)", new Object[]{details.getClientId(), szDetails});
        return false;
    }

当我调用方法“loadClientByCLientId”时,我收到异常

@Configuration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        migrateClients(args[0]);
    }

    private static void migrateClients(String clientId) {
        if (clientId.isEmpty()) {
            throw new RuntimeException("client id should be set as program argument");
        }
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        ClassicClientsService clientDetailsService = context.getBean(ClassicClientsService.class);
        OauthClientsManager oauthClientsManager = context.getBean(OauthClientsManager.class);
        clientDetailsService.loadClientByClientId(clientId);

日志:


    NFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
    at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:935)
    at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:374)
java database serialization deserialization rowmapper
1个回答
0
投票

对于面临这个问题的任何人,我遇到了同样的问题,发现它是由表的字符集和排序规则引起的,我在 persistence.xml 选项上设置了它并解决了,在我的例子中它使用的是 utf8mb4。

© www.soinside.com 2019 - 2024. All rights reserved.