问题描述
我正在研究一个Java项目,以使用GridFS规范从MongoDB存储和检索文件。我正在使用https://mongodb.github.io/mongo-java-driver/4.1/driver/tutorials/gridfs/中的MongoDB Java驱动程序文档中提供的代码段。
在使用 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));
有什么解决办法吗?
解决方法
看一下实现GridFSDownloadStreamImpl
的类GridFSDownloadStream
,看来方法read(byte[])
会逐块读取:
@Override
public int read(final byte[] b) {
return read(b,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();
请注意,我无法测试上述代码,因此请谨慎使用。
,我最终使用了 readAllBytes()方法,它返回了整个文件。
GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
int fileLength = (int) downloadStream.getGridFSFile().getLength();
byte[] bytesToWriteTo = new byte[fileLength];
bytesToWriteTo = downloadStream.readAllBytes();
downloadStream.close();