minor code refactor

This commit is contained in:
hxuanyu 2020-06-04 13:25:17 +10:00
parent d999f18f93
commit 8de01e2860

View File

@ -60,12 +60,11 @@ public class GridFsManager {
* @throws IOException 异常
*/
public void store(File localFile, String bucketName, String fileName) throws IOException {
if (db == null) {
return;
}
GridFSBucket bucket = getBucket(bucketName);
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(localFile))) {
bucket.uploadFromStream(fileName, bis);
if (available()) {
GridFSBucket bucket = getBucket(bucketName);
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(localFile))) {
bucket.uploadFromStream(fileName, bis);
}
}
}
@ -77,18 +76,17 @@ public class GridFsManager {
* @throws IOException 异常
*/
public void download(File targetFile, String bucketName, String fileName) throws IOException {
if (db == null) {
return;
}
GridFSBucket bucket = getBucket(bucketName);
byte[] buffer = new byte[1024];
try (GridFSDownloadStream gis = bucket.openDownloadStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile))
) {
while (gis.read(buffer) != -1) {
bos.write(buffer);
if (available()) {
GridFSBucket bucket = getBucket(bucketName);
byte[] buffer = new byte[1024];
try (GridFSDownloadStream gis = bucket.openDownloadStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile))
) {
while (gis.read(buffer) != -1) {
bos.write(buffer);
}
bos.flush();
}
bos.flush();
}
}