字节与 Oracle 数据库和原始文件上的 clob 数据不同

Bytes are different with clob data on Oracle db and original file

我有一个 png 文件,它在 Oracle 数据库中存储为 CLOB。

我想使用下面的代码 jdbc 模板获取这些数据

LobHandler lob = new DefaultLobHandler();
return jdbcTemplate.queryForObject(
  "select table from column",
  arr,
  (rs, rowNum) -> lob.getClobAsString(rs, "CLOB_DATA")
);

我正在按如下方式转换为字节数组,因为 oracle db 中的编码是 WE8MSWIN1252

clob.getBytes(Charset.forName("windows-1252"));

然而,当我使用以下代码手动读取文件并与数据库数据进行比较时,我得到了不同的字节。

File path = new File("path/to/file");
Files.readAllBytes(path.toPath());

有些字符没有正确加载。可能是什么问题?

I have a png file which is stored as CLOB in oracle db.

不要这样做。如果您有二进制数据,则将其存储为二进制格式,例如 BLOB,并且不要将其存储为不适合该目的的格式。

however i get the different bytes when i read the file manually with below code and compare with the db data.

那是因为您将 CLOB 用于不适合的用途。

what could be the problem?

问题是您为数据使用了错误的数据类型。

i cannot change it to blob since there are already added data as CLOB in DB.

将它们全部更改为 BLOB,然后当您需要获取字符数据时只需 convert the BLOB to a CLOB。尝试以相反的方式进行(不对二进制文件进行任何编码以使其可以安全地存储为字符串)将继续产生与您已经遇到的问题类似的问题。

如果你真的必须使用CLOB(请不要)然后以编码格式存储二进制文件,例如,可以安全地存储为字符数据。