Mongo version is 3.4.7
Using GridFS access picture
The code below
/* *
* save the file
* @ param collectionName
* @ param mongoFile
*/
Public Boolean saveFiles (String collectionName, MongoFile MongoFile) {
Try {
//DB DB=mongoTemplate. GetDb ();
//DBCollection mongdb_pic=the getCollection (collectionName);
//DBObject DBObject=new BasicDBObject ();
//if (mongoFile getFile ()!=null) {
.//byte [] bytes=FileUtil file2byte (mongoFile. GetFile ());
//
//Binary Binary=new Binary (bytes);
//dbObject. Put (" imgByte, "binary);
//}
//dbObject. Put (" id ", mongoFile getId ());
//dbObject. Put (" fileName ", mongoFile getFileName ());
//dbObject. Put (" fileSize, mongoFile getFileSize ());
//dbObject. Put (" descs ", mongoFile getDescs ());
//dbObject. Put (" fileType, mongoFile getFileType ());
//dbObject. Put (" contentType ", mongoFile getContentType ());
//dbObject. Put (" oldName ", mongoFile getContentType ());
//mongdb_pic. Save (dbObject);
The DB DB=mongoTemplate. GetDb ();
The GridFS GridFS=new GridFS (db, collectionName);
GridFSInputFile GFS=gridFS. CreateFile (mongoFile getFile ());
System. The out. Println (" file size: "+ mongoFile. GetFile (). The length ());
GFS. SetChunkSize (mongoFile. GetFile (). The length ());
GFS. SetFilename (mongoFile getFileName ());
GFS. Put (" _id ", mongoFile getId ());
GFS. Put (" fileName ", mongoFile getFileName ());
GFS. Put (" fileSize, "mongoFile getFileSize ());
GFS. Put (" descs ", mongoFile getDescs ());
GFS. Put (" fileType, "mongoFile getFileType ());
GFS. Put (" contentType, "mongoFile getContentType ());
GFS. Put (" oldName ", mongoFile getOldFileName ());
GFS. The save ();
} the catch (Exception e) {
Data logger. The error (" Mongo operation & gt;> Storing files sent wrong & gt;> Error: "+ e. oString ());
e.printStackTrace();
return false;
}
return true;
}
/* *
* the query file
* @ param collectionName
* @ param filename
* @ return
*/
Public GridFSDBFile retrieveFileOne (String collectionName, String filename) {
Try {
The DB DB=mongoTemplate. GetDb ();
The GridFS GridFS=new GridFS (db, collectionName);
GridFSDBFile dbfile=gridFS. FindOne (filename);
If (dbfile!=null) {
Return dbfile;
}
} the catch (Exception e) {
Data logger. The error (" Mongo operation & gt;> Get the error to the file & gt;> Error: "+ e. oString ());
e.printStackTrace();
}
return null;
}
CodePudding user response:
Use mongofiles to see how long will take picture whenCodePudding user response: