spring配置mongodb使用gridfs

参考的博文(相当详细):http://www.baeldung.com/spring-data-mongodb-gridfs

参考spring mongodb的官方文档:https://docs.spring.io/spring-data/mongodb/docs/2.0.2.RELEASE/reference/html/#gridfs


参考spring-data官网的配置

  
  
         
	
	
	
    
    
    
	
		
	
	
	
	
	
	
	
    
    
    
        
    
    
    
    
    	
    	
  	
      


属性文件

db.port=27017
db.host=127.0.0.1
db.user=dev
db.pwd=123456
db.name=isdb
db.replica-set=127.0.0.1:27017



单元测试例子
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsCriteria;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.bluedon.useradmin.datainteraction.commontype.CollectionName;
import com.bluedon.useradmin.datainteraction.model.bd.BDCommendLatitude;
import com.bluedon.useradmin.datainteraction.model.bd.metadata.BDCommend;
import com.bluedon.useradmin.datainteraction.util.FileUtils;
import com.mongodb.WriteConcern;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.client.gridfs.GridFSFindIterable;
import com.mongodb.client.gridfs.model.GridFSFile;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/application-context-test.xml"})
public class mongodbTest {
	@Resource  
    protected MongoTemplate mongoTemplate;
	
	@Resource
    GridFsOperations gridOperations;
	
	@Resource
	MongoDbFactory mongodbFactory;
	
	private String collectionName = CollectionName.BD_CommendLatitude;
	
	private void insert(BDCommendLatitude object) throws Exception {
		mongoTemplate.insert(object, collectionName);
	}
	
	@Test
	public void test() throws Exception {
		mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
		Date start = new Date();
		int i=0;
		while(i<10000) {
			i++;
			BDCommendLatitude obj = new BDCommendLatitude();
			
			BDCommend commend = new BDCommend();
			commend.setBzjlbh("123");
			commend.setHjrq(new Date());
			commend.setJlbfz("123456");
			commend.setJlhdz("456132");
			commend.setJlmc("123456");
			commend.setJlwh("123456");
			
			obj.setCommend(commend);
			insert(obj);
			System.out.println("插入完成:"+i);
		}
		Date end = new Date();
		System.out.println("执行完毕,用时(秒):" + (end.getTime() - start.getTime()) / 1000);
	}
	
	@Test
	public void storeFileToGridFs() throws IOException {

		BDCommend commend = new BDCommend();
		commend.setBzjlbh("123");
		commend.setHjrq(new Date());
		commend.setJlbfz("123456");
		commend.setJlhdz("456132");
		commend.setJlmc("123456");
		commend.setJlwh("filename.txt");
	    
		String filePath="C:\\Users\\liu\\Desktop\\待办.txt";
		File sourcefile = new File(filePath);
		// 读取文件内容 (输入流)  
		InputStream out = new FileInputStream(sourcefile);
		
		//上传文件,存储到gridfs
        String id = gridOperations.store(out, "filename.txt").toString();
        
        //查找上传的文件
        GridFSFile gridFsdbFile = gridOperations.findOne(new Query(GridFsCriteria.where("_id").is(id)));
        
        //通过ID获取资源文件
        InputStream downloadStream = GridFSBuckets.create(mongodbFactory.getDb()).openDownloadStream(gridFsdbFile.getObjectId());
        GridFsResource resource = new GridFsResource(gridFsdbFile, downloadStream);
        
        
        //获取流并写入到文件
        InputStream gridStream = resource.getInputStream();
        
        //读取字节流
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		byte[] buf = new byte[1024 * 100];// 100KB
		int len = 0;
		while ((len = gridStream.read(buf)) != -1) {
			buffer.write(buf, 0, len);	
		}
		
		FileUtils.writeFile("f:\\aa.txt", buffer.toByteArray());
	}
	
	@Test
	public void findFilesInGridFs() {
	    GridFSFindIterable result = null;
	    result = gridOperations.find(new Query(Criteria.where("filename").is("filename.txt")));
	    
	    //result = gridOperations.findOne(new Query(GridFsCriteria.whereFilename().is( "filename.txt")));
	}
	
	@Test
	public void readFilesFromGridFs() {
		//GridFSFile gridFsdbFile = gridOperations.findOne(new Query(GridFsCriteria.whereFilename().is( "filename.txt")));
		GridFsResource[] txtFiles = gridOperations.getResources("*.txt");
	}
}



你可能感兴趣的:(mongodb)