2019独角兽企业重金招聘Python工程师标准>>>
package com.amarsoft.lab.mongo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Assert;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
public class MyGridFsTest {
private String host = "127.0.0.1";
private int port = 27017;
private String dbName = "demogridfs";
/**
* 上传文件测试
* @throws UnknownHostException
* @throws FileNotFoundException
*/
@Test
public void putFileTest() throws UnknownHostException,
FileNotFoundException {
Mongo connection = new Mongo(host, port);
DB db = connection.getDB(dbName);
GridFS gridFs = new GridFS(db,"myfs");
String fileName = "/Users/yangsong/win-share/ams/ref/api-docs/JDK_API_1_6_zh_CN.CHM";
String name = "JDK_API_1_6_zh_CN";
InputStream in = new FileInputStream(fileName);
GridFSInputFile gridFSInputFile = gridFs.createFile(in);
gridFSInputFile.put("fcu", "tsir");
gridFSInputFile.setFilename(name);
gridFSInputFile.save();
}
/**
* 根据文件名查找文件
* @throws IOException
*/
@Test
public void testFindFile() throws IOException{
Mongo connection = new Mongo(host, port);
DB db = connection.getDB(dbName);
GridFS gridFs = new GridFS(db);
//按名称查找文件
String fileName = "/Users/yangsong/Documents/tmp/JDK_API_1_6_zh_CN-out.chm";
String name = "JDK_API_1_6_zh_CN";
GridFSDBFile gfsFile = getByFileName(name,gridFs);
File outFile = new File(fileName);
// if(!outFile.exists())outFile.createNewFile();
gfsFile.writeTo(outFile);
/**
* 测试删除文件
*/
DBObject query = new BasicDBObject("filename", fileName);
gridFs.remove(query);
gfsFile = getByFileName(name,gridFs);
Assert.assertNull(gfsFile);
}
/**
* 根据MD5码查找文件
* @throws IOException
*/
@Test
public void testFindFileByMd5() throws IOException{
Mongo connection = new Mongo(host, port);
DB db = connection.getDB(dbName);
GridFS gridFs = new GridFS(db);
String fileName = "/Users/yangsong/Documents/tmp/JDK_API_1_6_zh_CN-out.chm";
GridFSDBFile gfsFile = getByMd5("df03c9cf37c5fbc5a7894e04297127d7",gridFs);
File outFile = new File(fileName);
gfsFile.writeTo(outFile);
System.out.println("content-type:"+gfsFile.getContentType());
}
/**
* 计算文件的MD5码,看与数据库中存储的是否一致
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void testCalcFileMd5() throws FileNotFoundException, IOException{
String fileName = "/Users/yangsong/win-share/ams/ref/api-docs/JDK_API_1_6_zh_CN.CHM";
File file = new File(fileName);
String md5 = DigestUtils.md5Hex(new FileInputStream(file));
System.out.println(md5);
}
public GridFSDBFile getById(Object id, GridFS fs) {
DBObject query = new BasicDBObject("_id", id);
GridFSDBFile gridFSDBFile = fs.findOne(query);
return gridFSDBFile;
}
public GridFSDBFile getByFileName(String fileName, GridFS fs) {
DBObject query = new BasicDBObject("filename", fileName);
GridFSDBFile gridFSDBFile = fs.findOne(query);
return gridFSDBFile;
}
public GridFSDBFile getByMd5(String md5, GridFS fs) {
DBObject query = new BasicDBObject("md5", md5);
GridFSDBFile gridFSDBFile = fs.findOne(query);
return gridFSDBFile;
}
}