| 
					
				 | 
			
			
				@ -0,0 +1,453 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				package com.yihu.hos.web.framework.util; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.fasterxml.jackson.databind.ObjectMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.BasicDBObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.FindIterable; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.MongoCursor; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.MongoDatabase; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.gridfs.GridFSBucket; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.gridfs.GridFSBuckets; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.gridfs.GridFSFindIterable; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.gridfs.GridFSUploadStream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.gridfs.model.GridFSFile; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.gridfs.model.GridFSUploadOptions; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.mongodb.client.model.Filters; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.ehr.dbhelper.mongodb.MongodbFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.ehr.dbhelper.mongodb.MongodbHelper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.hos.core.file.FileUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import eu.medsea.mimeutil.MimeUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.bson.Document; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.bson.conversions.Bson; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.bson.types.ObjectId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.json.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.io.ByteArrayOutputStream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.io.File; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.io.IOException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.sql.Blob; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 *  MongoDB GridFS 操作类 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * @author HZY 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * @vsrsion 1.0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * Created at 2016/7/21. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				public class GridFSUtil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static final int defaultChunkSize = 1024 * 1024 * 4; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static ObjectId uploadFile(String dbName, Blob blob,String fileType,Map<String,Object> params) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //获取mongodb连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        MongodbHelper mongoOrigin = new MongodbHelper(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //创建一个容器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoDatabase db = MongodbFactory.getDB(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSBucket gridFS = GridFSBuckets.create(db); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String fileName=UUID.randomUUID().toString()+"."+fileType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //自定义字段 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Document metaDocument= new Document(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if ( params!=null && params.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for (Map.Entry<String, Object> entry : params.entrySet()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String key=entry.getKey(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                metaDocument.append(key,entry.getValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        // Create some custom options 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                .chunkSizeBytes(defaultChunkSize).metadata(metaDocument); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSUploadStream uploadStream = gridFS.openUploadStream(fileName, gridFSUploadOptions); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            byte[] data = FileUtil.toByteArray(blob.getBinaryStream()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            uploadStream.write(data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            ObjectId id = uploadStream.getFileId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (id != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                return id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (uploadStream != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                uploadStream.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *  上传文件至Mongodb by GridFS 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param dbName    数据库名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param filePath  文件路径 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param params    自定义保存字段 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static boolean uploadFile(String dbName, String filePath,Map<String,Object> params) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //获取mongodb连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        MongodbHelper mongoOrigin = new MongodbHelper(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //创建一个容器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoDatabase db = MongodbFactory.getDB(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSBucket gridFS = GridFSBuckets.create(db); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        File readFile = new File(filePath); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //自定义字段 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Document metaDocument= new Document(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if ( params!=null && params.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for (Map.Entry<String, Object> entry : params.entrySet()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String key=entry.getKey(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                metaDocument.append(key,entry.getValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        // Create some custom options 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                .chunkSizeBytes(defaultChunkSize).metadata(metaDocument); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSUploadStream uploadStream = gridFS.openUploadStream(readFile.getName(), gridFSUploadOptions); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            byte[] data = FileUtil.toByteArray(filePath); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            uploadStream.write(data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            ObjectId id = uploadStream.getFileId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (id != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (uploadStream != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                uploadStream.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 从 mongodb GridFS 下载文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param dbName    数据库名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param savePath 文件保存路径 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param objId        GridFS文件保存ObjectId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static String downFile(String dbName, String savePath, ObjectId objId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //穿件mongodb连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        MongodbHelper mongoOrigin = new MongodbHelper(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //创建一个容器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoDatabase db = MongodbFactory.getDB(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSBucket gridFS = GridFSBuckets.create(db); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        gridFS.downloadToStream(objId, out); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            boolean succ = FileUtil.writeFile(savePath, out.toByteArray(), "utf-8"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (succ) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                return savePath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (out != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    out.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *  批量下载文件保存(根据 fs.files集合) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param dbName    数据库名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param savePath  文件保存的路径 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param fsFiles     fs.files 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 以“,”分割的文件名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static Map<String,StringBuffer> downFileList(String dbName, String savePath, List<GridFSFile> fsFiles) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        StringBuffer stringBuffer = new StringBuffer(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Map<String,String> fileNames= new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //穿件mongodb连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        MongodbHelper mongoOrigin = new MongodbHelper(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //创建一个容器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoDatabase db = MongodbFactory.getDB(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSBucket gridFS = GridFSBuckets.create(db); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (fsFiles!=null && fsFiles.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for (GridFSFile fsFile:fsFiles){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                ObjectId objId = fsFile.getObjectId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String fileType= fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf(".")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String newName = UUID.randomUUID().toString()+"."+fileType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                gridFS.downloadToStream(objId, out); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    boolean succ = FileUtil.writeFile(savePath+"/"+newName, out.toByteArray(), "utf-8"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if (succ) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        String type = getMimeType( out.toByteArray()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        fileNames.put(newName,type); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//                        stringBuffer.append(newName).append(","); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if (out != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            out.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return groupDataMap(fileNames); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static Map<String,StringBuffer> downFilesByObjectIds(String dbName, String savePath, List<ObjectId> ids) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        StringBuffer stringBuffer = new StringBuffer(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Map<String,String> fileNames= new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //穿件mongodb连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        MongodbHelper mongoOrigin = new MongodbHelper(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //创建一个容器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoDatabase db = MongodbFactory.getDB(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSBucket gridFS = GridFSBuckets.create(db); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<GridFSFile> fsFiles = findFsFiles(dbName, ids); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (fsFiles!=null && fsFiles.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for (GridFSFile fsFile:fsFiles){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                ObjectId objId = fsFile.getObjectId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String fileType= fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf(".")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String newName = UUID.randomUUID().toString()+"."+fileType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                gridFS.downloadToStream(objId, out); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    boolean succ = FileUtil.writeFile(savePath+"/"+newName, out.toByteArray(), "utf-8"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if (succ) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        String type = getMimeType( out.toByteArray()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        fileNames.put(newName,type); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//                        stringBuffer.append(newName).append(","); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if (out != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            out.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return groupDataMap(fileNames); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *  删除 mongodb-GridFS文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param dbName 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param objId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static void deleteFile(String dbName, ObjectId objId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        MongodbHelper mongoOrigin = new MongodbHelper(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoDatabase db = MongodbFactory.getDB(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSBucket gridFS = GridFSBuckets.create(db); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        gridFS.delete(objId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 查询fs.files 数据 in GridFS 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param dbName 数据库名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param filters 查询条件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return files集合 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static List<GridFSFile> findFiles(String dbName,Map<String,Object> filters){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //穿件mongodb连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<GridFSFile> list=new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //创建一个容器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoDatabase db = MongodbFactory.getDB(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSBucket gridFSBucket = GridFSBuckets.create(db); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<Bson> querys=new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //添加查询条件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (filters!=null && filters.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for (Map.Entry<String, Object> entry : filters.entrySet()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String key=entry.getKey(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                querys.add(Filters.eq(key, entry.getValue())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        GridFSFindIterable gsIterable=gridFSBucket.find(Filters.eq("metadata.cda_id", "111")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSFindIterable gsIterable=gridFSBucket.find(Filters.and(querys)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       MongoCursor<GridFSFile> it= gsIterable.iterator(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        while (it.hasNext()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            GridFSFile fsFile= it.next(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            list.add(fsFile); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return list; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *  根据ObjectID集合查询GridFS 文件列表 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param dbName 数据库名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param ids     objectId集合 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static List<GridFSFile> findFsFiles(String dbName,List<ObjectId> ids){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //穿件mongodb连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<GridFSFile> list=new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //创建一个容器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoDatabase db = MongodbFactory.getDB(dbName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSBucket gridFSBucket = GridFSBuckets.create(db); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<Bson> querys=new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //添加查询条件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (ids!=null && ids.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            querys.add(Filters.in("_id", ids)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        GridFSFindIterable gsIterable=null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (querys.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            gsIterable=gridFSBucket.find(Filters.and(querys)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            gsIterable=gridFSBucket.find(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoCursor<GridFSFile> it= gsIterable.iterator(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        while (it.hasNext()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            GridFSFile fsFile= it.next(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            list.add(fsFile); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return list; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static Map parseJsonToMap(JSONObject condition) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        ObjectMapper objectMapper = new ObjectMapper(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        HashMap map = new HashMap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                Iterator fileNames = (Iterator) condition.names(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                while(fileNames.hasNext()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String fieldName = (String)fileNames.next(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    Object valueNode = condition.get(fieldName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if(valueNode instanceof Blob) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        map.put(fieldName, valueNode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        map.put(fieldName, condition.get(fieldName).toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } catch (Exception var7) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                var7.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    //TODO  测试所用,后删除 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static void tTestFind(String tableName, String collection, Map<String,Object> params){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongodbHelper mongodbHelper = new MongodbHelper(tableName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        BasicDBObject basicDBObject=new BasicDBObject(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        for (Map.Entry<String, Object> entry : params.entrySet()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String key=entry.getKey(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            basicDBObject.append(key,entry.getValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        FindIterable<Document> documents=mongodbHelper.query(collection,basicDBObject, null, null); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MongoCursor<Document> it= documents.iterator(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        while (it.hasNext()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            Document document= it.next(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<Document> fileList= (List<Document>) document.get("files"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            Document jsonNode=fileList.get(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List files= (List) jsonNode.get("files_id"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<GridFSFile> fs=findFsFiles("mydb",files); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            System.out.println(files.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String filePath="e:/test/"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            Map<String,StringBuffer> path=downFileList("mydb",filePath, fs); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static void main(String[] args) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //上传 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            Map<String,Object> params= new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            params.put("cda_id","111"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            params.put("report_id","1001"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            params.put("patient_id","1001"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            params.put("event_no","1001"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            String fileName = "e:/test/肺2.jpg"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            uploadFile("mydb", fileName,params); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //下载 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            String filePath="e:/test/xiao_copy.jpg"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            downFile("mydb",filePath,"579067939724e11514b2eead"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //删除 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            deleteFile("mydb","57906e369724e11dd8e75798"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //查询 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            List<GridFSFile> fsFiles  = findFiles("mydb",params); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            // 根据objectId集合 查询 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            List<ObjectId> list=new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            list.add(new ObjectId("5791c9399724e12b9437b229")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            list.add(new ObjectId("5795aab07a2b9020ec19fdfa")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            List<GridFSFile> fs=findFsFiles("mydb",list); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //测试查询类 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            tTestFind("document","CDA_TEST",params); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            //批量下载 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            String filePath="e:/test/"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//            downFileList("mydb",filePath, fs); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /*************************************** MineType 工具类 *********************************/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 获取文件Mine-Type 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param file 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static String getMimeType(File file) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Collection<?> collection=MimeUtil.getMimeTypes(file); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return collection.toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static String getMimeType(byte[] bytes) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Collection<?> collection=MimeUtil.getMimeTypes(bytes); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return collection.toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 非结构化档案--文件类型map生成 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param map 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static Map<String, StringBuffer> groupDataMap(Map<String, String> map) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Map<String, StringBuffer> result = new HashMap<String, StringBuffer>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Iterator<String> rs=map.keySet().iterator(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        while (rs.hasNext()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String key = rs.next(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value = map.get(key); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (result.containsKey(value)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                result.get(value).append(",").append(key); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                result.put(value, new StringBuffer(key)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				} 
			 |