【问题标题】:Incomplete file returned by GridFSGridFS 返回的文件不完整
【发布时间】:2020-09-10 11:26:51
【问题描述】:
我正在开发一个使用 GridFS 规范从 MongoDB 存储和检索文件的 Java 项目。我正在使用 https://mongodb.github.io/mongo-java-driver/4.1/driver/tutorials/gridfs/ 的 MongoDB Java 驱动程序文档中提供的代码 sn-ps。
在使用OpenDownloadStream检索文件时,我注意到如果文件被分成多个块,它只返回第一个块,而不是完整的文件。
ObjectId fileId;
GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
int fileLength = (int) downloadStream.getGridFSFile().getLength();
byte[] bytesToWriteTo = new byte[fileLength];
downloadStream.read(bytesToWriteTo); /*read file contents */
downloadStream.close();
System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));
有什么解决办法吗?
【问题讨论】:
标签:
java
mongodb
gridfs
mongo-java-driver
【解决方案1】:
查看实现GridFSDownloadStream的类GridFSDownloadStreamImpl,看起来方法read(byte[])是逐块读取的:
@Override
public int read(final byte[] b) {
return read(b, 0, b.length);
}
@Override
public int read(final byte[] b, final int off, final int len) {
checkClosed();
if (currentPosition == length) {
return -1;
} else if (buffer == null) {
buffer = getBuffer(chunkIndex);
} else if (bufferOffset == buffer.length) {
chunkIndex += 1;
buffer = getBuffer(chunkIndex);
bufferOffset = 0;
}
int r = Math.min(len, buffer.length - bufferOffset);
System.arraycopy(buffer, bufferOffset, b, off, r);
bufferOffset += r;
currentPosition += r;
return r;
}
因此,您必须循环直到实际读取所有预期的字节:
byte[] bytesToWriteTo = new byte[fileLength];
int bytesRead = 0;
while(bytesRead < fileLength) {
int newBytesRead = downloadStream.read(bytesToWriteTo);
if(newBytesRead == -1) {
throw new Exception();
}
bytesRead += newBytesRead;
}
downloadStream.close();
请注意,我无法测试上述代码,因此请谨慎使用。
【解决方案2】:
我最终使用了 readAllBytes() 方法,它返回了整个文件。
GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
int fileLength = (int) downloadStream.getGridFSFile().getLength();
byte[] bytesToWriteTo = new byte[fileLength];
bytesToWriteTo = downloadStream.readAllBytes();
downloadStream.close();