MongoDB的常用工具类以及文件上传
pom.xml添加依赖
<!-- mongodb -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.14.3</version>
</dependency>
mongodb.properties 配置文件
#mongodb
mongodb.server=172.16.8.2
mongodb.port = 27017
mongodb.dbname = TEST
MongoDB工具类
package com.sm.share3d.utils;
import com.mongodb.*;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import org.bson.types.ObjectId;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
/**
* MongoDB工具类
* @author wangxueqing
*
*/
public class MongoDBUtil {
private static final Integer soTimeOut = 300000;
private static final Integer connectionsPerHost = 500;
private static final Integer threadsAllowedToBlockForConnectionMultiplier = 500;
public static String MONGO_SERVER = "172.16.8.2";
public static int MONGO_PORT = 27017;
public static String MONGO_DBNAME = "";
private static DB dbConnection = null;
private static Map<String, DBCollection> dbCollectionMap = new ConcurrentHashMap<String, DBCollection>();
private static MongoClient mongoClient = null;
static {
try {
// 读取配置文件
Properties prop = new Properties();
prop.load(MongoDBUtil.class.getClassLoader().getResourceAsStream("mongodb.properties"));
// 根据 key 获取 value
MONGO_SERVER = prop.getProperty("mongodb.server");
MONGO_PORT = Integer.parseInt(prop.getProperty("mongodb.port"));
MONGO_DBNAME = prop.getProperty("mongodb.dbname");
} catch (IOException e) {
e.printStackTrace();
System.err.println("读取mongo.properties出错");
}
}
/**
* 获取连接
* @Title: getMongoClient
* @param @return 参数
* @return MongoClient 返回类型
* @throws
*/
public static MongoClient getMongoClient() {
if (mongoClient == null) {
try {
//mongoClient = new MongoClient(MONGO_SERVER, MONGO_PORT);
mongoClient = new MongoClient(new ServerAddress(MONGO_SERVER, MONGO_PORT),
new MongoClientOptions.Builder().socketTimeout(soTimeOut).connectionsPerHost(connectionsPerHost)
.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier)
.socketKeepAlive(true).build());
if (null != mongoClient) {
dbConnection = mongoClient.getDB(MONGO_DBNAME);
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
return mongoClient;
}
/**
* 获取默认DB
* @Title: getDB
* @param @param dbname
* @param @return 参数
* @return DB 返回类型
* @throws
*/
public static DB getDB() {
MongoDBUtil.getMongoClient();
DB db = mongoClient.getDB(MONGO_DBNAME);
return db;
}
/**
* 获取指定DB实例
* @Title: getDB
* @param @param dbname
* @param @return 参数
* @return DB 返回类型
* @throws
*/
public static DB getDB(String dbname) {
MongoDBUtil.getMongoClient();
if (dbname != null && !"".equals(dbname)) {
DB db = mongoClient.getDB(dbname);
return db;
}
return null;
}
/**
* 获取指定集合
* @Title: getDBCollection
* @param @param collectionName
* @param @return 参数
* @return DBCollection 返回类型
* @throws
*/
public static DBCollection getDBCollection(String collectionName) {
MongoDBUtil.getMongoClient();
DBCollection collection = null;
if (dbCollectionMap.containsKey(collectionName)) {
collection = dbCollectionMap.get(collectionName);
} else {
collection = MongoDBUtil.dbConnection.getCollection(collectionName);
if (null != collection) {
dbCollectionMap.put(collectionName, collection);
}
}
return collection;
}
/**
* 判断指定文档是否存在
* @Title: isDocumentExsit
* @param @param collectionName
* @param @param query
* @param @return 参数
* @return boolean 返回类型
* @throws
*/
public static boolean isDocumentExsit(String collectionName, DBObject query) {
boolean result = false;
DBCursor dbCursor = null;
DBCollection collection = MongoDBUtil.getDBCollection(collectionName);
if (null != collection) {
dbCursor = collection.find(query);
if (null != dbCursor && dbCursor.hasNext()) {
result = true;
}
}
return result;
}
/**
* 查询指定集合中的指定文档
* @Title: selectDocument
* @param @param collectionName
* @param @param query
* @param @return 参数
* @return List<DBObject> 返回类型
* @throws
*/
public static List<DBObject> selectDocument(String collectionName, DBObject query) {
List<DBObject> result = new ArrayList<>();
DBCursor dbCursor = null;
DBCollection collection = MongoDBUtil.getDBCollection(collectionName);
if (null != collection) {
dbCursor = collection.find(query);
if (null != dbCursor) {
while (dbCursor.hasNext()) {
result.add(dbCursor.next());
}
}
}
return result;
}
/**
* 查询指定集合中的所有文档
* @Title: selectAll
* @param @param collectionName
* @param @return 参数
* @return List<DBObject> 返回类型
* @throws
*/
public static List<DBObject> selectAll(String collectionName) {
List<DBObject> result = new ArrayList<>();
DBCursor dbCursor = null;
DBCollection collection = MongoDBUtil.getDBCollection(collectionName);
if (null != collection) {
dbCursor = collection.find();
if (null != dbCursor) {
while (dbCursor.hasNext()) {
result.add(dbCursor.next());
}
}
}
return result;
}
/**
* 在指定集合中添加文档
* @Title: insertDocument
* @param @param collectionName
* @param @param newDocument 参数
* @return void 返回类型
* @throws
*/
public static void insertDocument(String collectionName, DBObject newDocument) {
DBCollection collection = MongoDBUtil.getDBCollection(collectionName);
if (null != collection) {
if (!MongoDBUtil.isDocumentExsit(collectionName, newDocument)) {
collection.insert(newDocument);
}
}
}
/**
* 在指定集合中批量添加数据
* @Title: insertDocument
* @param @param collectionName
* @param @param documentList 参数
* @return void 返回类型
* @throws
*/
public static void insertDocument(String collectionName, List<DBObject> documentList) {
DBCollection collection = MongoDBUtil.getDBCollection(collectionName);
if (null != collection) {
collection.insert(documentList);
}
}
/**
* 更新文档信息
* @Title: updateDocument
* @param @param collectionName
* @param @param query
* @param @param updatedDocument
* @param @return 参数
* @return boolean 返回类型
* @throws
*/
public static boolean updateDocument(String collectionName, DBObject query, DBObject updatedDocument) {
boolean result = false;
WriteResult writeResult = null;
DBCollection collection = MongoDBUtil.getDBCollection(collectionName);
if (null != collection) {
writeResult = collection.update(query, updatedDocument);
if (null != writeResult) {
if (writeResult.getN() > 0) {
result = true;
}
}
}
return result;
}
/**
*
* @Title: deleteDocument
* @param @param collectionName
* @param @param query
* @param @return 参数
* @return boolean 返回类型
* @throws
*/
public static boolean deleteDocument(String collectionName, DBObject query) {
boolean result = false;
WriteResult writeResult = null;
DBCollection collection = MongoDBUtil.getDBCollection(collectionName);
if (null != collection) {
writeResult = collection.remove(query);
if (null != writeResult) {
if (writeResult.getN() > 0) {
result = true;
}
}
}
return result;
}
/**
* 查询文档信息
* @Title: selectDocument
* @param @param collectionName
* @param @param query查询条件
* @param @param keys要返回的字段
* @param @param sorter排序字段
* @param @return 参数
* @return List<DBObject> 返回类型
* @throws
*/
public static List<DBObject> selectDocument(String collectionName,DBObject query, DBObject keys, DBObject sorter) {
List<DBObject> result = new ArrayList<>();
DBCursor dbCursor = null;
DBCollection collection = MongoDBUtil.getDBCollection(collectionName);
if (null != collection) {
dbCursor = collection.find(query, keys);
if (sorter != null) {
dbCursor = dbCursor.sort(sorter);
}
if (null != dbCursor) {
result = dbCursor.toArray();
}
}
return result;
}
/**
* 存储文件信息
* @Title: SaveFile
* @param @param collectionName 集合名称
* @param @param file 保存的文件
* @param @param filename 保存的文件名
* @param @return 参数
* @return GridFSInputFile 返回类型
* @throws
*/
public GridFSInputFile saveFile(String collectionName, File file, String filename) {
try {
DB db = MongoDBUtil.dbConnection;
// 存储fs的根节点
GridFS gridFS = new GridFS(db, collectionName);
GridFSInputFile gfs = gridFS.createFile(file);
gfs.put("filename", filename);
gfs.put("contentType", filename.substring(filename.lastIndexOf(".")));
gfs.save();
return gfs;
} catch (Exception e) {
e.printStackTrace();
System.out.println("存储文件时发生错误!!!");
}
return null;
}
/**
* 根据文件名查询文件信息
* @Title: getByFileName
* @param @param collectionName 集合名称
* @param @param filename 保存的文件名
* @param @return 参数
* @return GridFSDBFile 返回类型
* @throws
*/
public GridFSDBFile getByFileName(String collectionName, String filename) {
try {
DB db = MongoDBUtil.dbConnection;
// 获取fs的根节点
GridFS gridFS = new GridFS(db, collectionName);
GridFSDBFile dbfile = gridFS.findOne(filename);
if (dbfile != null) {
return dbfile;
}
} catch (Exception e) {
}
return null;
}
/**
* 根据文件ID查询文件信息
* @Title: getById
* @param @param collectionName
* @param @param id
* @param @return 参数
* @return GridFSDBFile 返回类型
* @throws
*/
public GridFSDBFile getById(String collectionName, String id) {
try {
DB db = MongoDBUtil.dbConnection;
// 获取fs的根节点
GridFS gridFS = new GridFS(db, collectionName);
GridFSDBFile dbfile = gridFS.findOne(new ObjectId(id));
if (dbfile != null) {
return dbfile;
}
} catch (Exception e) {
}
return null;
}
public static void main(String[] args) {
//BasicDBObject document = new BasicDBObject();
//document.put("id", 1002);
//document.put("msg", "hello world mongoDB");
//将新建立的document保存到collection中去
//MongoDBUtil.insertDocument("zdfiles", document);
// 存储文件信息
try {
String fileName = "2-140922163HNH.jpg";
File readFile = new File("C:\\Users\\imooc\\Pictures\\"+fileName);
// GridFSInputFile gfsFile = gridFS.createFile(readFile);
// gfsFile.setFilename(fileName);
// gfsFile.save();
GridFS gridFS = new GridFS(MongoDBUtil.getDB(), "contracts_files");
// 读取文件
GridFSDBFile outFile = gridFS.findOne(fileName);
System.out.println(outFile.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
MongoDB的文件操作
package com.sm.share3d.controller;
import com.mongodb.BasicDBObject;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import com.sm.share3d.bean.R;
import com.sm.share3d.service.MongodbService;
import com.sm.share3d.utils.MongoDBUtil;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* MongoDB Controller
*
* @author wangxueqing
*/
@Controller
@RequestMapping("/common")
public class CommonController {
@Autowired
private MongodbService mongodbService;
/**
* 上传文件并保存到Mongo中 单个文件
*/
@RequestMapping("/uploadFile")
@ResponseBody
public R uploadFile(MultipartFile file, HttpServletRequest request, String uploadModule,String remark) {
// Users user = new User();
//如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/");
// 获取图片文件
MultipartFile mFile = file;
if (mFile != null) {
// 保存到mongodb
String myFileName = mFile.getOriginalFilename();
// 存储文件信息
GridFS gridFS = new GridFS(MongoDBUtil.getDB(), uploadModule + "_files");
try {
// 重命名上传后的文件名
String newFileName = "mongoDBupload" + mFile.getOriginalFilename();
// 定义上传路径
String path = realPath + myFileName;
File localFile = new File(path);
mFile.transferTo(localFile);
GridFSInputFile oneFile = gridFS.createFile(localFile);
oneFile.setFilename(myFileName);
// 配置文件属性
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
BasicDBObject metadata = new BasicDBObject();
metadata.put("uploadDate", df.format(new Date()));
// metadata.put("uploadUserId", user.getPkid());
// metadata.put("uploadUserName", user.getUserName());
// metadata.put("uploadDpetId", user.getDeptId());
metadata.put("uploadSysModule", uploadModule);
metadata.put("remark", remark);
oneFile.setMetaData(metadata); //添加属性
oneFile.save(); //保存文件
// 上传完成删除文件
localFile.delete();
// 返回信息
Object id1 = oneFile.getId();
return R.ok().put("id", id1.toString());
} catch (IOException e) {
e.printStackTrace();
}
} else {
return R.error();
}
return null;
}
/**
* 批量上传文件
*/
@RequestMapping("/uploadFiles")
@ResponseBody
public R uploadFiles(@RequestParam("files") MultipartFile[] files, HttpServletRequest request, String uploadModule, String id,String remark) {
try {
for (int i = 0; i < files.length; i++) {
R r = uploadFile(files[i], request, uploadModule,remark);
String fileId = String.valueOf(r.get("id"));
mongodbService.saveMongodbFileRef(id, fileId);
}
return R.ok().put("code", 200);
} catch (Exception e) {
return R.error();
}
}
/**
* 删除文件并删除mongo
*/
@RequestMapping("/deleteFile")
@ResponseBody
public R deleteFile(ModelMap model, HttpServletRequest request) {
if (request.getParameter("id") != null) {
// 系统模块
String uploadModule = request.getParameter("uploadModule");
// 存储文件信息
GridFS gridFS = new GridFS(MongoDBUtil.getDB(), uploadModule + "_files");
// 读取文件
GridFSDBFile outFile = gridFS.findOne(new ObjectId(request.getParameter("id")));
//System.out.println(outFile);
if (outFile != null) {
gridFS.remove(outFile);
}
} else {
R.error();
}
return R.ok();
}
/**
* 通过id查询文件信息
*/
@RequestMapping("/getFile")
@ResponseBody
public R getFile(ModelMap model, HttpServletRequest request) {
if (request.getParameter("id") != null && !"".equals(request.getParameter("id"))) {
// 系统模块
String uploadModule = request.getParameter("uploadModule");
// 存储文件信息
GridFS gridFS = new GridFS(MongoDBUtil.getDB(), uploadModule + "_files");
try {
// 读取文件
GridFSDBFile outFile = gridFS.findOne(new ObjectId(request.getParameter("id")));
//System.out.println(outFile);
if (outFile != null) {
return R.ok().put("fileName", outFile.getFilename());
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
R.error();
}
return R.ok();
}
/**
* 根据文件ID查询图片
*/
@RequestMapping(value = "/imgShow")
public void show(HttpServletRequest request, HttpServletResponse response) {
try {
OutputStream out = response.getOutputStream();
//设置图片无缓存
response.setHeader("Cache-Control", "no-store, no-cache");
//设置以图片的形式返回给页面
response.setContentType("image/jpeg");
// 系统模块
String uploadModule = request.getParameter("uploadModule");
GridFS gridFS = new GridFS(MongoDBUtil.getDB(), uploadModule + "_files");
// 读取文件
GridFSDBFile outFile = gridFS.findOne(new ObjectId(request.getParameter("id")));
if (outFile != null) {
outFile.writeTo(out);
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据文件ID查询pdf
*/
@RequestMapping(value = "/pdfShow")
@ResponseBody
public R pdfShow(HttpServletRequest request, HttpServletResponse response) {
try {
OutputStream out = response.getOutputStream();
String uploadDir = request.getSession().getServletContext().getRealPath("/") + "/" + "uploadDir/";
File fileDir = new File(uploadDir);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
//设置图片无缓存
response.setHeader("Cache-Control", "no-store, no-cache");
//设置以图片的形式返回给页面
response.setContentType("application/pdf");
// 系统模块
String uploadModule = request.getParameter("uploadModule");
GridFS gridFS = new GridFS(MongoDBUtil.getDB(), uploadModule + "_files");
// 读取文件
GridFSDBFile outFile = gridFS.findOne(new ObjectId(request.getParameter("id")));
if (outFile != null) {
// outFile.writeTo(out);
outFile.writeTo(new File(uploadDir + outFile.getFilename()));
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return R.ok();
}
@RequestMapping(value = "/downloadFile")
@ResponseBody
public void downloadFile(HttpServletRequest request,HttpServletResponse response){
try {
request.setCharacterEncoding("UTF-8");
String uploadModule = request.getParameter("uploadModule");
GridFS gridFS = new GridFS(MongoDBUtil.getDB(), uploadModule + "_files");
// 读取文件
GridFSDBFile outFile = gridFS.findOne(new ObjectId(request.getParameter("id")));
String filename = outFile.getFilename();
long length = outFile.getLength();
response.setContentType("text/html;charset=UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment; filename=" + new String(filename.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(length));
OutputStream out = response.getOutputStream();
if (outFile != null) {
outFile.writeTo(out);
out.flush();
out.close();
}
}catch (Exception e){
}
}
@RequestMapping(value = "/downloadZIP")
@ResponseBody
public void downloadZIP(HttpServletRequest request,HttpServletResponse response){
try {
request.setCharacterEncoding("UTF-8");
String pkid = request.getParameter("pkid");
String uploadModule = request.getParameter("uploadModule");
String tmpName = rename("临时文件.zip");
String ctxPath = request.getSession().getServletContext().getRealPath("/")+"/" + "uploadDir/";
String zipName = zipName(ctxPath+"/"+tmpName);
ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
GridFS gridFS = new GridFS(MongoDBUtil.getDB(), uploadModule + "_files");
List list = mongodbService.getMongodbFileRef(pkid);
InputStream inputStream = null;
for(Object o : list){
if(o != null){
String fileId = o.toString();
// 读取文件
GridFSDBFile outFile = gridFS.findOne(new ObjectId(fileId));
String filename = outFile.getFilename();
long length = outFile.getLength();
zipOutputStream.putNextEntry(new ZipEntry(filename));
zipOutputStream.setEncoding("GBK");
inputStream = outFile.getInputStream();
int temp = 0;
while((temp = inputStream.read()) != -1){
zipOutputStream.write(temp);
}
inputStream.close();
}
}
zipOutputStream.close();
File file = new File(zipName);
response.setContentType("text/html;charset=UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment; filename=" + new String(tmpName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(file.length()));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
bis = new BufferedInputStream(new FileInputStream(zipName));
OutputStream out = response.getOutputStream();
bos = new BufferedOutputStream(out);
byte[] buff = new byte[51990260];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
File file1 = new File(zipName);
if(file1.exists()){
file1.delete();
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 将上传的文件进行重命名
*
* @author geloin
* @date 2012-3-29 下午3:39:53
* @param name
* @return
*/
private static String rename(String name) {
Long now = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
Long random = (long) (Math.random() * now);
String fileName = now + "" + random;
if (name.indexOf(".") != -1) {
fileName += name.substring(name.lastIndexOf("."));
}
return fileName;
}
/**
* 压缩后的文件名
*
* @author geloin
* @date 2012-3-29 下午6:21:32
* @param name
* @return
*/
private static String zipName(String name) {
String prefix = "";
if (name.indexOf(".") != -1) {
prefix = name.substring(0, name.lastIndexOf("."));
} else {
prefix = name;
}
return prefix + ".zip";
}
}