我想把我的文件保存得尽可能小。所以我决定把我的json文件压缩。我目前使用的方法是在文件夹中创建gzip文件、上传mongodb和删除文件,但我注意到gridfs具有将输入流保存到数据库的功能。
我的问题
如果我所有的问题都能回答的话。我想知道如何使用gridfs Inputstream并将我的gzip代码转换为GridFS。
电流码
try {
GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream("pdiskio.dsv"));
OutputStreamWriter osw = new OutputStreamWriter(gzip, StandardCharsets.UTF_8);
osw.write(saves.toJSONString());
osw.close();
} catch (IOException e) {
e.printStackTrace();
}发布于 2020-05-14 07:00:22
经过一些搜索和尝试,我学会了如何正确地使用它,而不用我的文件夹数据。我想分享我的示例代码,然后是搜索它的人。
这里是示例代码
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(byteArrayOutputStream);
gzip.write(saves.toJSONString().getBytes(StandardCharsets.UTF_8));
gzip.close();
GridFSBucket gridFSFilesBucket = GridFSBuckets.create(database, "pdisks");
GridFSUploadOptions options = new GridFSUploadOptions()
.metadata(new Document("metadataValue", "test-value"));
InputStream ff = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectId fileId = gridFSFilesBucket.uploadFromStream(minewatchSaveManager.getId(), ff, options);https://stackoverflow.com/questions/61790660
复制相似问题