Explorar el Código

GridFsUtil修改

demon hace 8 años
padre
commit
2c96a15cd4

+ 2 - 2
hos-broker/src/main/java/com/yihu/hos/broker/services/camel/ESBCamelService.java

@ -254,7 +254,7 @@ public class ESBCamelService {
//        String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD);
//        MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
//
//        boolean read = GridFSUtil.readFile(db, outputStream, fileName);
//        boolean read = GridFSUtil.readFileContent(db, outputStream, fileName);
//        if (!read) {
//            logger.error("not mongo file, fileName:" + fileName);
//            return false;
@ -274,7 +274,7 @@ public class ESBCamelService {
            //编译成功后将class文件上传至mongodb;文件名为类名+ routeCode
            String packagePath = StringUtil.replaceStrAll(handleFile.getPackageName(), ".", "/");
            String classPath =  resource.getPath()+ packagePath + "/" + handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
            GridFSUtil.uploadFile(dbName, classPath, handleFile.getClassName()+handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE);
            GridFSUtil.uploadFile(classPath, handleFile.getClassName()+handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE,null);
        }
        return succ;
    }

+ 2 - 2
hos-rest/src/main/java/com/yihu/hos/rest/services/crawler/CollectHelper.java

@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFSDBFile;
import com.yihu.ehr.dbhelper.mongodb.MongodbHelper;
import com.yihu.hos.core.datatype.DateUtil;
import com.yihu.hos.core.file.FileUtil;
@ -116,7 +116,7 @@ public class CollectHelper {
                documentNode = objectMapper.createObjectNode();
                List files = (List) document.get("files_id");
                String cdaDocumentId = (String) document.get("cda_doc_id");
                List<GridFSFile> fs = GridFSUtil.findFsFiles(dbGridFS, files);
                List<GridFSDBFile> fs = GridFSUtil.findFsFiles(dbGridFS, files);
                String filePath = getDocumentDir(patient);
                Map<String, StringBuffer> map = GridFSUtil.downFileList("mydb", filePath, fs);//�����
                for (Map.Entry<String, StringBuffer> entry : map.entrySet()) {

+ 1 - 1
hos-rest/src/main/java/com/yihu/hos/rest/services/crawler/CrawlerService.java

@ -599,7 +599,7 @@ public class CrawlerService {
                    Blob blob=(Blob)jsonObject.get("CONTENT");
                    String type= (String) jsonObject.get("REPORT_TYPE");
                    try {
                        ObjectId objectId= GridFSUtil.uploadFile("files", blob, type, null);
                        ObjectId objectId= GridFSUtil.uploadFile( blob, type, null);
                        jsonObject.put("CONTENT",objectId);
//                        InputStream in=blob.getBinaryStream();
//                        String path="e:/test/blob/"+UUID.randomUUID().toString()+"."+type;

+ 1 - 1
hos-rest/src/main/java/com/yihu/hos/rest/services/crawler/DatacollectService.java

@ -959,7 +959,7 @@ public class DatacollectService {
                        params.put("patient_id", patientId);
                        params.put("event_no", eventNo);
                        try {
                            ObjectId objectId = GridFSUtil.uploadFile("files", blob, type, params);
                            ObjectId objectId = GridFSUtil.uploadFile( blob, type, params);
                            jsonObject.put("CONTENT", objectId);
                        } catch (Exception e) {
                            e.printStackTrace();

+ 40 - 132
hos-web-framework/src/main/java/com/yihu/hos/web/framework/util/GridFSUtil.java

@ -3,8 +3,6 @@ package com.yihu.hos.web.framework.util;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.client.gridfs.GridFSUploadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
import com.mongodb.gridfs.GridFSDBFile;
import com.yihu.hos.core.file.FileUtil;
@ -16,7 +14,6 @@ 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.stereotype.Component;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.annotation.PostConstruct;
import java.io.*;
@ -46,7 +43,7 @@ public class GridFSUtil {
        gridFsOperations = this.operations;
    }
    public static ObjectId uploadFile(String dbName, Blob blob, String fileType, Map<String, Object> params) {
    public static ObjectId uploadFile( Blob blob, String fileType, Map<String, Object> params) {
//        String fileName = UUID.randomUUID().toString() + "." + fileType;
        //自定义字段
        Document metaDocument = new Document();
@ -74,11 +71,11 @@ public class GridFSUtil {
    /**
     * 上传文件至Mongodb by GridFS
     *
     * @param dbName   数据库名
     * @param filePath 文件路径
     * @param params   自定义保存字段
     * @param filePath        上传的文件路径
     * @param saveFileName   保存到mongo的文件名
     * @param params          自定义保存字段
     */
    public static boolean uploadFile(String dbName, String filePath, Map<String, Object> params) {
    public static String uploadFile( String filePath,String saveFileName, Map<String, Object> params) {
        //自定义字段
        Document metaDocument = new Document();
        if (params != null && params.size() > 0) {
@ -88,15 +85,13 @@ public class GridFSUtil {
            }
        }
        // Create some custom options
        GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
                .chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
        FileInputStream fileInputStream = null;
        try {
            File readFile = new File(filePath);
            fileInputStream = new FileInputStream(readFile);
            com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(fileInputStream, gridFSUploadOptions);
            com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(fileInputStream,saveFileName,"",metaDocument);
            if (gridFSFile != null) {
                return true;
                return saveFileName;
            }
        } catch (IOException e) {
            e.printStackTrace();
@ -109,7 +104,7 @@ public class GridFSUtil {
                }
            }
        }
        return false;
        return null;
    }
    /**
@ -151,13 +146,13 @@ public class GridFSUtil {
     * @param fsFiles  fs.files
     * @return 以“,”分割的文件名
     */
    public static Map<String, StringBuffer> downFileList(String dbName, String savePath, List<GridFSFile> fsFiles) {
    public static Map<String, StringBuffer> downFileList(String dbName, String savePath, List<GridFSDBFile> fsFiles) {
        StringBuffer stringBuffer = new StringBuffer();
        Map<String, String> fileNames = new HashMap<>();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (fsFiles != null && fsFiles.size() > 0) {
            for (GridFSFile fsFile : fsFiles) {
                ObjectId objectId = fsFile.getObjectId();
            for (GridFSDBFile fsFile : fsFiles) {
                Object objectId = fsFile.getId();
                String fileType = fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
                String newName = UUID.randomUUID().toString() + "." + fileType;
                GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("_id").is(objectId)));
@ -166,7 +161,6 @@ public class GridFSUtil {
                    boolean success = FileUtil.writeFile(savePath + "/" + newName, out.toByteArray(), "utf-8");
                    if (!success) {
                        break;
                    }
                    String type = getMimeType(out.toByteArray());
@ -190,45 +184,6 @@ public class GridFSUtil {
        }
    }
    public static Map<String, StringBuffer> downFilesByObjectIds(String dbName, String savePath, List<ObjectId> ids) {
        StringBuffer stringBuffer = new StringBuffer();
        Map<String, String> fileNames = new HashMap<>();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        List<GridFSDBFile> fsFiles = findFsFiles(dbName, ids);
        if (fsFiles != null && fsFiles.size() > 0) {
            for (GridFSDBFile fsFile : fsFiles) {
                ObjectId objectId = (ObjectId) fsFile.getId();
                String fileType = fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
                String newName = UUID.randomUUID().toString() + "." + fileType;
                GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("_id").is(objectId)));
                try {
                    gridFSDBFile.writeTo(out);
                    boolean success = FileUtil.writeFile(savePath + "/" + newName, out.toByteArray(), "utf-8");
                    if (!success) {
                        break;
                    }
                    String type = getMimeType(out.toByteArray());
                    fileNames.put(newName, type);
                } 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文件
@ -243,11 +198,10 @@ public class GridFSUtil {
    /**
     * 查询fs.files 数据 in GridFS
     *
     * @param dbName  数据库名
     * @param filters 查询条件
     * @return files集合
     */
    public static List<GridFSDBFile> findFiles(String dbName, Map<String, Object> filters) {
    public static List<GridFSDBFile> findFiles( Map<String, Object> filters) {
        Query query = new Query();
        if (filters != null) {
            filters.forEach((key, value) -> query.addCriteria(GridFsCriteria.where(key).is(value)));
@ -314,89 +268,39 @@ public class GridFSUtil {
        return result;
    }
    /**
     * 上传文件至Mongodb by GridFS
     *
     * @param db   数据库
     * @param file 文件
     *  上传文件至Mongodb by GridFS
     * @param saveFileName  保存到mongo的文件名
     * @param inputStream   文件流
     * @param params         metaData数据
     * @return
     */
    public static String uploadFile(MongoDatabase db, CommonsMultipartFile file) {
        //创建一个容器
        GridFSBucket gridFS = GridFSBuckets.create(db);
        //自定义字段
    public static String uploadFile( InputStream inputStream,String saveFileName,Map<String ,Object> params) {
        //metaData参数
        Document metaDocument = new Document();
        // Create some custom options
        GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
                .chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
        String fileName = UUID.randomUUID() + file.getFileItem().getName();
        GridFSUploadStream uploadStream = gridFS.openUploadStream(fileName, gridFSUploadOptions);
        try {
            byte[] data = file.getBytes();
            uploadStream.write(data);
            ObjectId id = uploadStream.getFileId();
            if (id != null) {
                return fileName;
            }
        } finally {
            if (uploadStream != null) {
                uploadStream.close();
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                metaDocument.append(key, entry.getValue());
            }
        }
        return "";
    }
    /**
     * 上传文件至Mongodb by GridFS
     *
     * @param dbName   数据库名
     * @param filePath 文件路径
     */
    public static String uploadFile(String dbName, String filePath, String fileName) {
        //自定义字段
        Document metaDocument = new Document();
        // Create some custom options
        GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
                .chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
        FileInputStream fileInputStream = null;
        // Create some custom options
//        GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
//                .chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
//        String fileName = UUID.randomUUID() + file.getFileItem().getName();
//        GridFSUploadStream uploadStream = gridFS.openUploadStream(fileName, gridFSUploadOptions);
//        try {
//            byte[] data = FileUtil.toByteArray(filePath);
//            uploadStream.write(data);
//            ObjectId id = uploadStream.getFileId();
//            if (id != null) {
//                return fileName;
//            }
//        } finally {
//            if (uploadStream != null) {
//                uploadStream.close();
//            }
//        }
        try {
            fileInputStream = new FileInputStream(filePath);
            com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(fileInputStream, gridFSUploadOptions);
            ObjectId id = (ObjectId) gridFSFile.getId();
            if (id != null) {
                return fileName;
            com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(inputStream, saveFileName, "", metaDocument);
            if (gridFSFile != null) {
                return saveFileName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
            if (inputStream != null) {
                try {
                    fileInputStream.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        return "";
    }
    /**
@ -420,11 +324,10 @@ public class GridFSUtil {
    /**
     * 读取文件内容
     *
     * @param dbName   数据库名
     * @param fileName 文件名
     * @return
     */
    public static String readFile(String dbName, String fileName) {
    public static String readFileContent( String fileName) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("filename").is(fileName)));
@ -445,14 +348,19 @@ public class GridFSUtil {
     * @param fileName GridFS文件名
     * @return
     */
    public static String downFile(String dbName, String savePath, String fileName) {
    public static String downFile( String savePath, String fileName) {
        FileOutputStream fileOutputStream = null;
        try {
            File file = new File(savePath);
            fileOutputStream = new FileOutputStream(file);
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("").is(fileName)));
            gridFSDBFile.writeTo(fileOutputStream);
            return savePath;
            List<GridFSDBFile> gridFSDBFiles = gridFsOperations.find(Query.query(GridFsCriteria.where("filename").is(fileName)));
            if (gridFSDBFiles==null || gridFSDBFiles.isEmpty()){
                return null;
            }else {
                GridFSDBFile gridFSDBFile = gridFSDBFiles.get(0);
                gridFSDBFile.writeTo(fileOutputStream);
                return savePath;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

+ 2 - 2
src/main/java/com/yihu/hos/common/CollectHelper.java

@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFSDBFile;
import com.yihu.ehr.dbhelper.mongodb.MongodbHelper;
import com.yihu.hos.core.datatype.DateUtil;
import com.yihu.hos.core.file.FileUtil;
@ -116,7 +116,7 @@ public class CollectHelper {
                documentNode = objectMapper.createObjectNode();
                List files = (List) document.get("files_id");
                String cdaDocumentId = (String) document.get("cda_doc_id");
                List<GridFSFile> fs = GridFSUtil.findFsFiles(dbGridFS, files);
                List<GridFSDBFile> fs = GridFSUtil.findFsFiles(dbGridFS, files);
                String filePath = getDocumentDir(patient);
                Map<String, StringBuffer> map = GridFSUtil.downFileList("mydb", filePath, fs);//�����
                for (Map.Entry<String, StringBuffer> entry : map.entrySet()) {

+ 1 - 1
src/main/java/com/yihu/hos/datacollect/service/DatacollectService.java

@ -968,7 +968,7 @@ public class DatacollectService  {
                        params.put("patient_id",patientId);
                        params.put("event_no",eventNo);
                        try {
                            ObjectId objectId = GridFSUtil.uploadFile("files", blob, type, params);
                            ObjectId objectId = GridFSUtil.uploadFile( blob, type, params);
                            jsonObject.put("CONTENT", objectId);
                        } catch (Exception e) {
                            e.printStackTrace();

+ 3 - 2
src/main/java/com/yihu/hos/system/service/AppManager.java

@ -17,6 +17,7 @@ import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.annotation.Resource;
import java.io.OutputStream;
import java.util.Map;
import java.util.UUID;
/**
 * Created by chenweida on 2016/1/21.
@ -133,10 +134,10 @@ public class AppManager {
   
    public Result uploadFile(CommonsMultipartFile file) {
        String dbName = "upload";
        String newFileName;
        try {
            newFileName = GridFSUtil.uploadFile(mongoConfig.mongoClient().getDatabase(dbName), file);
            String fileName = UUID.randomUUID() + file.getFileItem().getName();
            newFileName = GridFSUtil.uploadFile(file.getInputStream() ,fileName,null);
            if (!StringUtil.isEmpty(newFileName)) {
                return Result.success(DES.encrypt(newFileName, DES.COMMON_PASSWORD));
            }

+ 9 - 12
src/main/java/com/yihu/hos/system/service/FlowManager.java

@ -24,10 +24,7 @@ import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.annotation.Resource;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
 * 系统流程管理业务类
@ -66,11 +63,11 @@ public class FlowManager {
            String newFileName = className + routeId + ".java";
            String newFilePath = "/temp/" + className;
            String text = GridFSUtil.readFile("upload", tempFilePath);
            String text = GridFSUtil.readFileContent(tempFilePath);
            boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
            //TODO 上传到GridFS
            if (succ) {
                newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
                newFileName = GridFSUtil.uploadFile( newFilePath, newFileName,null);
                return newFileName;
            }
        } catch (Exception e) {
@ -93,7 +90,7 @@ public class FlowManager {
        try {
            String newFileName = className + routeId + ".java";
            String newFilePath = "/temp/" + newFileName;
            String text = GridFSUtil.readFile("upload", tempFilePath);
            String text = GridFSUtil.readFileContent( tempFilePath);
            if (text.contains("?cron=")) {
                String oldStr = text.substring(text.indexOf("?cron=") + 6);
                String cron = oldStr.substring(0, oldStr.indexOf("\""));
@ -108,7 +105,7 @@ public class FlowManager {
            boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
            //TODO 上传到GridFS
            if (succ) {
                newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
                newFileName = GridFSUtil.uploadFile( newFilePath, newFileName,null);
            } else {
                return null;
            }
@ -135,12 +132,12 @@ public class FlowManager {
            String newFileName = className + routeId + ".java";
            String newFilePath = "/temp/" + className;
            String text = GridFSUtil.readFile("upload", tempFilePath);
            String text = GridFSUtil.readFileContent( tempFilePath);
            text = text.replace("jobId=jobId", "jobId=" + jobId);
            boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
            //TODO 上传到GridFS
            if (succ) {
                newFileName = GridFSUtil.uploadFile("upload", newFilePath, newFileName);
                newFileName = GridFSUtil.uploadFile( newFilePath, newFileName,null);
                return newFileName;
            }
        } catch (Exception e) {
@ -403,10 +400,10 @@ public class FlowManager {
    public Result uploadFile(CommonsMultipartFile file) {
        String dbName = "upload";
        String newFileName;
        try {
            newFileName = GridFSUtil.uploadFile(mongoConfig.mongoClient().getDatabase(dbName), file);
            String fileName = UUID.randomUUID() + file.getFileItem().getName();
            newFileName = GridFSUtil.uploadFile(file.getInputStream(),fileName,null);
            if (!StringUtil.isEmpty(newFileName)) {
                return Result.success(DES.encrypt(newFileName, DES.COMMON_PASSWORD));
            }

+ 4 - 1
src/main/java/com/yihu/hos/tenant/service/TenantService.java

@ -7,6 +7,7 @@ import com.yihu.hos.tenant.model.TenantModel;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.GridFSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -32,6 +33,9 @@ public class TenantService {
    @Autowired
    private MongoConfig mongoConfig;
    @Value("${spring.data.mongodb.gridFsDatabase}")
    private String dbName;
    public TenantModel findTenantByName(String name) throws Exception {
        List<TenantModel> list = tenantDao.getTenantList(name);
@ -82,7 +86,6 @@ public class TenantService {
    }
    public Result readFile(OutputStream os, String fileName) {
        String dbName = "upload";
        try {
            fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
            GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);

+ 2 - 2
src/test/java/com/yihu/hos/HosAdminApplicationTests.java

@ -18,9 +18,9 @@ public class HosAdminApplicationTests {
	public void contextLoads() throws IOException {
		String newName = "text"+System.currentTimeMillis()+".java";
		String newFilePath = "/upload/"+newName;
		String  content = GridFSUtil.readFile("upload", "955569af-43fb-422f-bded-d88f99cad1f9QuartzRoute.java");
		String  content = GridFSUtil.readFileContent( "955569af-43fb-422f-bded-d88f99cad1f9QuartzRoute.java");
		boolean succ = FileUtil.writeFile(newFilePath, content, "UTF-8");
		newName = GridFSUtil.uploadFile("upload", newFilePath, newName);
		newName = GridFSUtil.uploadFile( newFilePath, newName,null);
		System.out.println(content);
	}