gpt4 book ai didi

java - 无法使用 java 从 MySQL 数据库 longblob 列获取 zip 文件

转载 作者:行者123 更新时间:2023-11-29 18:27:04 26 4
gpt4 key购买 nike

我试图从数据库中检索 zip,但每次生成的 zip 都会损坏。该 zip 应该包含 3 个 pdf 文件,但当我生成它时,它只包含第一个大小为 0 的文件,并且当我尝试打开 zip 时,会显示“存档意外结束”错误弹出窗口。我无法弄清楚出了什么问题,因为数据库中的文件没有损坏,并且代码可以在我的电脑和许多其他远程服务器上运行,但不能在特定的远程服务器上运行(全部运行在 Wildfly 10 上,相同的 mysql 数据库配置,与数据库中存储的相同 zip)。我的代码如下(JDBC):

 ...
Statement stmt = conn.createStatement();
ResultSet rsstmt.executeQuery("SELECT document_data from table "
+ "WHERE condition");

if (rs.next() && rs.getBytes("document_data") != null) {
ByteArrayInputStream(rs.getBytes("document_data"));
File zipped= new File("exported.zip");
FileOutputStream fos = new FileOutputStream(zipped);

byte[] buffer = new byte[1];
InputStream is = rs.getBinaryStream(1);
while (is.read(buffer) > 0) {
fos.write(buffer);
}
fos.close();
}

我也尝试了以下代码,但也不起作用:

InputStream in = null;//zip bytes
OutputStream out;//zip archive to be generated
.
.
.
if (rs.next() && rs.getBytes("document_data") != null) {
in = new ByteArrayInputStream(rs.getBytes("document_data"));
}
out = new FileOutputStream("exported.zip");
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();

执行以下 SQL 查询时,生成的 zip 未损坏:

SELECT document_data INTO DUMPFILE '/tmp/exported.zip' FROM table WHERE condition;

注意:数据库中的 zip 大小(LONGBLOB 字段)为 830K。

注意:我尝试使用 JDBC 和 Hibernate,但结果是相同的。

对这种奇怪的行为有什么想法吗?

最佳答案

您都在调用 getBytes(),它会读出 blob,然后从其输入流中读取数据,此时该输入流将为空。

摆脱 getBytes() 调用。

关于java - 无法使用 java 从 MySQL 数据库 longblob 列获取 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46094250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com