5.15 GridFS support
MongoDB supports storing binary files inside it's filesystem GridFS. Spring Data MongoDB provides a GridFsOperations
interface as well as the according implementation GridFsTemplate
to easily interact with the filesystem. You can setup a GridFsTemplate
instance by handing it a MongoDbFactory
as well as aMongoConverter
:
Example 5.16. JavaConfig setup for a GridFsTemplate
class GridFsConfiguration extends AbstractMongoConfiguration { // … further configuration omitted @Bean public GridFsTemplate gridFsTemplate() { return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter()); } }
An according XML configuration looks like this:
Example 5.17. XML configuration for a GridFsTemplate
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <mongo:db-factory id="mongoDbFactory" dbname="database" /> <mongo:mapping-converter id="converter" /> <bean class="org.springframework.data.mongodb.gridfs.GridFsTemplate"> <constructor-arg ref="mongoDbFactory" /> <constructor-arg ref="converter" /> </bean> </beans>
You can no get the template injected and perform storing and retrieving operations to it.
Example 5.18. Using GridFsTemplate to store files
class GridFsClient { @Autowired GridFsOperations operations; @Test public void storeFileToGridFs { FileMetadata metadata = new FileMetadata(); // populate metadata Resource file = … // lookup File or Resource operations.store(file.getInputStream(), "filename.txt", metadata); } }
The store(…)
operations take an InputStream
, a filename and optionally metadata information about the file to store. The metadata can be an arbitrary object which will be marshalled by the MongoConverter
configured with the GridFsTemplate
. Alternatively you can also provide a DBObject
as well.
Reading files from the filesystem can either be achieved through the find(…)
or getResources(…)
methods. Let's have a look at the find(…)
methods first. You can either find a single file matching a Query
or multiple ones. To easily define file queries we provide the GridFsCriteria
helper class. It provides static factory methods to encapsulate default metadata fields (e.g. whereFilename()
, whereContentType()
) or the custom one through whereMetaData()
.
Example 5.19. Using GridFsTemplate to query for files
class GridFsClient { @Autowired GridFsOperations operations; @Test public void findFilesInGridFs { List<GridFSDBFile> result = operations.find(query(whereFilename().is("filename.txt"))) } }
Note | |
---|---|
Currently MongoDB does not support defining sort criterias when retrieving files from GridFS. Thus any sort criterias defined on the |
The other option to read files from the GridFs is using the methods introduced by the ResourcePatternResolver
interface. They allow handing an Ant path into the method ar thus retrieve files matching the given pattern.
Example 5.20. Using GridFsTemplate to read files
class GridFsClient { @Autowired GridFsOperations operations; @Test public void readFilesFromGridFs { GridFsResources[] txtFiles = operations.getResources("*.txt"); } }
GridFsOperations
extending ResourcePatternResolver
allows the GridFsTemplate
e.g. to be plugged into an ApplicationContext
to read Spring Config files from a MongoDB.
Spring Data MongoDB : Save Binary File, GridFS Example
GridFsTemplate
to store / read image in / from MongoDB.1. GridFS – Save example (Spring config in Annotation)
Gat an image file and save it into MongoDB.
package com.mkyong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.gridfs.GridFsTemplate; import com.mongodb.Mongo; import com.mongodb.MongoClient; /** * Spring MongoDB configuration file * */ @Configuration public class SpringMongoConfig extends AbstractMongoConfiguration{ @Bean public GridFsTemplate gridFsTemplate() throws Exception { return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter()); } @Override protected String getDatabaseName() { return "yourdb"; } @Override @Bean public Mongo mongo() throws Exception { return new MongoClient("127.0.0.1"); } }
package com.mkyong.core; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.mongodb.gridfs.GridFsOperations; import com.mkyong.config.SpringMongoConfig; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; /** * GridFs example * * @author mkyong * */ public class GridFsAppStore { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class); GridFsOperations gridOperations = (GridFsOperations) ctx.getBean("gridFsTemplate"); DBObject metaData = new BasicDBObject(); metaData.put("extra1", "anything 1"); metaData.put("extra2", "anything 2"); InputStream inputStream = null; try { inputStream = new FileInputStream("/Users/mkyong/Downloads/testing.png"); gridOperations.store(inputStream, "testing.png", "image/png", metaData); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("Done"); } }
Review the MongoDB console, see what is saved.
> show dbs yourdb 0.203125GB > use yourdb switched to db yourdb > show collections fs.chunks fs.files system.indexes > db.fs.files.find() { "_id" : ObjectId("51641c5630045c9b3db35afc"), "chunkSize" : NumberLong(262144), "length" : NumberLong(4238), "md5" : "9969527cd95a5a573f15e953f0036800", "filename" : "testing.png", "contentType" : "image/png", "uploadDate" : ISODate("2013-04-09T13:49:10.104Z"), "aliases" : null, "metadata" : { "extra1" : "anything 1", "extra2" : "anything 2" } } > > db.fs.chunks.find() { "_id" : ObjectId("51641c5630045c9b3db35afd"), "files_id" : ObjectId("51641c5630045c9b3db35afc"), "n" : 0, "data" : BinData(0,"/9j/4AAQSkZJRgABAgAAZ......EQH/9k=") }
The image’s information is saved in fs.files
, and the image’s file (converted in binary) is stored in fs.chunks
, and link via _id
and files_id
.
2. GridFS – Read example (Spring config in XML file)
Read above image file from MongoDB and save it as another image.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <mongo:db-factory id="mongoDbFactory" dbname="yourdb" /> <mongo:mapping-converter id="converter" /> <bean name="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate"> <constructor-arg ref="mongoDbFactory" /> <constructor-arg ref="converter" /> </bean> </beans>
package com.mkyong.core; import java.io.IOException; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.gridfs.GridFsOperations; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.gridfs.GridFSDBFile; /** * GridFs example * * @author mkyong * */ public class GridFsAppRead { public static void main(String[] args) { ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml"); GridFsOperations gridOperations = (GridFsOperations) ctx.getBean("gridFsTemplate"); List<GridFSDBFile> result = gridOperations.find( new Query().addCriteria(Criteria.where("filename").is("testing.png"))); for (GridFSDBFile file : result) { try { System.out.println(file.getFilename()); System.out.println(file.getContentType()); //save as another image file.writeTo("/Users/mkyong/Downloads/new-testing.png"); } catch (IOException e) { e.printStackTrace(); } } System.out.println("Done"); } }
Download Source Code
References
출처 - http://www.mkyong.com/mongodb/spring-data-mongodb-save-binary-file-gridfs-example/
Java MongoDB : Save Image Example
In this tutorial, we show you how to save an image file into MongoDB, via GridFS API. The GridFS APIs are able to serve other binary files as well, like video and music files.
For detail explanation, read this MongoDB GridFS manual.
1. Save image
Code snippets to save an image file into MongoDB, under “photo” namespace, and assign a new “filename” for the saved image.
String newFileName = "mkyong-java-image"; File imageFile = new File("c:\\JavaWebHosting.png"); GridFS gfsPhoto = new GridFS(db, "photo"); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(newFileName); gfsFile.save();
2. Get image
Code snippets to get the saved image by its “filename”.
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); System.out.println(imageForOutput);
Output, the image is saved as following JSON format.
{ "_id" : { "$oid" : "4dc9511a14a7d017fee35746" } , "chunkSize" : 262144 , "length" : 22672 , "md5" : "1462a6cfa27669af1d8d21c2d7dd1f8b" , "filename" : "mkyong-java-image" , "contentType" : null , "uploadDate" : { "$date" : "2011-05-10T14:52:10Z" } , "aliases" : null }
3. Print all saved images
Code snippets to get all the saved files from MongoDB and iterate it with DBCursor.
GridFS gfsPhoto = new GridFS(db, "photo"); DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); }
4. Save into another image
Code snippets to get an image file from MongoDB and output it to another image file.
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); //output to new file
5. Delete image
Code snippets to delete an image file.
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); gfsPhoto.remove(gfsPhoto.findOne(newFileName));
Full Example
Full example to work with image, via Java MongoDB GridFS API. See comments for explanation.
package com.mkyong.core; import java.io.File; import java.io.IOException; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; import com.mongodb.gridfs.GridFS; import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSInputFile; /** * Java MongoDB : Save image example * */ public class SaveImageApp { public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("imagedb"); DBCollection collection = db.getCollection("dummyColl"); String newFileName = "mkyong-java-image"; File imageFile = new File("c:\\JavaWebHosting.png"); // create a "photo" namespace GridFS gfsPhoto = new GridFS(db, "photo"); // get image file from local drive GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); // set a new filename for identify purpose gfsFile.setFilename(newFileName); // save the image file into mongoDB gfsFile.save(); // print the result DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); } // get image file by it's filename GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); // save it into a new image file imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); // remove the image file from mongoDB gfsPhoto.remove(gfsPhoto.findOne(newFileName)); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
At the end of the program, a new image file is created in “c:\\JavaWebHostingNew.png“.
Reference
출처 - http://www.mkyong.com/mongodb/java-mongodb-save-image-example/
'Framework & Platform > Spring' 카테고리의 다른 글
spring - @Cacheable 적용 범위 (0) | 2013.07.26 |
---|---|
spring - mvc file download(파일 다운로드) (0) | 2013.07.21 |
spring - Multiple File Upload Example (0) | 2013.07.17 |
spring data - mongoTemplate embedded document insert (0) | 2013.07.13 |
spring - Array @ModelAttribute expansion in Spring MVC (0) | 2013.06.27 |