上傳有兩種方式,一種是使用UploadFromStream,另外一種是OpenUploadStream方式,這里使用的是第二種方式:
public ObjectId
save (){GridFSBucket gfsbk = GridFSBuckets.create(db,
"user_photo" );
byte [] data =
"Data to upload into GridFS" .getBytes(StandardCharsets.UTF_8);GridFSUploadStream uploadStream = gfsbk .openUploadStream(
"user_09734" );uploadStream.write(data);ObjectId id = uploadStream.getFileId();uploadStream.close();
return id;}
解析: 1. GridFSBucket gfsbk = GridFSBuckets.create(db, “user_photo”);
創建一個容器,傳入一個`MongoDatabase`類實例db,和容器名稱"user_photo"作為參數
2. GridFSUploadStream uploadStream = gfsbk .openUploadStream(“user_09734”);
打開上傳流,傳入一個String參數作為GirdFS文件的文件名
3. ObjectId id = uploadStream.getFileId();
得到剛剛上傳的文件的id
Mongo Java驅動GirdFS上傳參考資料 下面是我編寫的上傳幫助類:
package
com .huanle .utils .db import java
.io .ByteArrayOutputStream
import java
.io .IOException import org
.bson .types .ObjectId
import org
.springframework .beans .factory .annotation .Autowired
import org
.springframework .stereotype .Component import
com .huanle .utils .model .FileEntity
import
com .mongodb .Block
import
com .mongodb .client .MongoDatabase
import
com .mongodb .client .gridfs .GridFSBucket
import
com .mongodb .client .gridfs .GridFSBuckets
import
com .mongodb .client .gridfs .GridFSUploadStream
import
com .mongodb .client .gridfs .model .GridFSFile
import
com .mongodb .gridfs .GridFS @Component
public class GirdFSHelper {private GridFSBucket gfsbkprivate MongoDatabase db@Autowiredpublic GirdFSHelper(MongoDBHelper mongoManager){this
.db = mongoManager
.getDB (
"huanle" )}public FileEntity save(String bucket,FileEntity file){gfsbk = GridFSBuckets
.create (db, bucket)GridFSUploadStream uploadStream = gfsbk
.openUploadStream (file
.getFileName ())uploadStream
.write (file
.getContent ())ObjectId id = uploadStream
.getFileId ()uploadStream
.flush ()uploadStream
.close ()FileEntity filter = new FileEntity()filter
.setId (id)filter = find(bucket,filter)return filter}public FileEntity download(String bucket,FileEntity filter) throws IOException{filter = find(bucket,filter)if(filter==null){return null}ByteArrayOutputStream
out = new ByteArrayOutputStream()gfsbk = GridFSBuckets
.create (db, bucket)gfsbk
.downloadToStream (filter
.getId (),
out )filter
.setContent (
out .toByteArray ())
out .close ()return filter}public FileEntity find(String bucket,FileEntity filter){gfsbk = GridFSBuckets
.create (db, bucket)System
.out .println (filter
.toBson ())GridFSFile result = gfsbk
.find (filter
.toBson ())
.first ()if(result==null){ return null}filter
.setId (result
.getObjectId ())filter
.setSize (result
.getLength ())filter
.setFileName (result
.getFilename ())filter
.setUploadTime (result
.getUploadDate ())return filter}}package
com .huanle .utils .model import java
.util .Date import javax
.validation .constraints .Past import org
.bson .Document
import org
.bson .types .ObjectId
import org
.hibernate .validator .constraints .Range import
com .huanle .utils .annotation .FileContent
import
com .huanle .utils .annotation .FileName
import
com .huanle .utils .annotation .PlainString
public class FileEntity {public static String ID=
"_id" ,FILE_NAME=
"fileName" ,SIZE=
"size" ,UPLOAD_TIME=
"uploadTime" ,CONTENT=
"content" private ObjectId id@PlainString@FileNameprivate String fileName@Range(max=
41943040 )private long size@Pastprivate Date uploadTime@FileContentprivate byte[] contentpublic Document toBson(){Document doc = new Document()if(this
.id !=null){doc
.append (ID, this
.id )}if(this
.fileName !=null){doc
.append (FILE_NAME, this
.fileName )}if(this
.size !=
0 ){doc
.append (SIZE, this
.size )}if(this
.uploadTime !=null){doc
.append (UPLOAD_TIME, this
.uploadTime )}return doc}}
總結
以上是生活随笔 為你收集整理的Java上传文件到MongoDB GirdFS 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。