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