Merge pull request #3 from hxuanyu/master

[opt] refined file copy
This commit is contained in:
Yukino 2020-06-04 14:10:34 +08:00 committed by GitHub
commit deacae9dd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,12 +78,13 @@ public class GridFsManager {
public void download(File targetFile, String bucketName, String fileName) throws IOException { public void download(File targetFile, String bucketName, String fileName) throws IOException {
if (available()) { if (available()) {
GridFSBucket bucket = getBucket(bucketName); GridFSBucket bucket = getBucket(bucketName);
byte[] buffer = new byte[1024];
try (GridFSDownloadStream gis = bucket.openDownloadStream(fileName); try (GridFSDownloadStream gis = bucket.openDownloadStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile)) BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile))
) { ) {
while (gis.read(buffer) != -1) { byte[] buffer = new byte[1024];
bos.write(buffer); int bytes = 0;
while ((bytes = gis.read(buffer)) != -1) {
bos.write(buffer, 0, bytes);
} }
bos.flush(); bos.flush();
} }