Searching...
Monday, 18 November 2013

Spring Data Mongodb: How to save files in Mongodb

09:27

Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for for new datastores while retaining store-specific features and capabilities. See more about SpringData

In this example we will see how to insert a file in mongodb using SpringData GridFSTemplate.

Prerequisites: Mongodb should up and running

1. Create a Maven Project

Create a Maven project and add below dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.pretech</groupId>
	<artifactId>spring-data-fs-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<repositories>
		<repository>
			<id>spring-milestone</id>
			<name>Spring Maven MILESTONE Repository</name>
			<url>http://maven.springframework.org/milestone</url>
		</repository>
	</repositories>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>
		<!-- mongodb java driver -->
		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>2.5.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
			<version>1.1.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2</version>
		</dependency>
	</dependencies>
</project>   

2. Create a Main class to create GridFSTemplate and insert files

package com.pretech;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsCriteria;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import com.mongodb.Mongo;
import com.mongodb.gridfs.GridFSDBFile;
public class SpringDataGridFSTamplate {
	@Bean
	public static MongoDbFactory mongoDbFactory() throws Exception {
		Mongo mongo = new Mongo("localhost");
		String databaseName = "myfiles";
		MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongo,
				databaseName);
		return mongoDbFactory;
	}
	@Bean
	public static GridFsTemplate gridFsTemplate() throws Exception {
		MongoDbFactory dbFactory = mongoDbFactory();
		MongoConverter converter = mongoConverter();
		GridFsTemplate gridFsTemplate = new GridFsTemplate(dbFactory, converter);
		return gridFsTemplate;
	}
	@Bean
	public static MongoConverter mongoConverter() throws Exception {
		MongoMappingContext mappingContext = new MongoMappingContext();
		MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(
				mongoDbFactory(), mappingContext);
		return mappingMongoConverter;
	}
	public static void main(String[] args) {
		try {
			InputStream is = new FileInputStream("customer.xlsx");
				gridFsTemplate().store(is, "customer.xlsx");
				List<GridFSDBFile> result = gridFsTemplate().find(
						new Query().addCriteria(GridFsCriteria.whereFilename().is(
								"customer.xlsx")));
				for (GridFSDBFile file : result) {
						file.writeTo("newcustomer.xlsx");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("Completed");
	}
}

Run it



In MongoDB


image


New customer file created in project folder


image


 


 


 

0 comments: