zoukankan      html  css  js  c++  java
  • 上传文件至GridFs

    /**
     * 上传文件至GridFs
     * @param gridFsTemplate
     * @param file
     */
    
    需要自行导入jar包
    public static ObjectId uploadFile(GridFsTemplate gridFsTemplate, MultipartFile file){
        try {
            //获取文件的MD5
            String md5 = DigestUtils.md5DigestAsHex(file.getInputStream());
            Query query = new Query();
            query.addCriteria(Criteria.where("md5").is(md5));
            //根据文件MD5值查询
            GridFSFile gridFSDBFile = gridFsTemplate.findOne(query);
            //如果不存在重复文件则存入GridFS
            if(CommentUtils.isEmpty(gridFSDBFile)) {
                log.info("upload file..");
                DBObject metaData = new BasicDBObject();
                metaData.put("createdDate", new Date());
                String fileName = file.getOriginalFilename();
                log.info("File Name: " + fileName);
                ObjectId objectId = gridFsTemplate.store(file.getInputStream(), fileName, file.getContentType(), metaData);
                log.info("File upload: " + file.getContentType());
                return objectId;
            }
            return gridFSDBFile.getObjectId();
        } catch (Exception e) {
            log.error("IOException: " + e);
            throw new BadRequestException(ExceptionEnum.UPLOAD_FILE_FILE.getMessage());
        }
    }
  • 相关阅读:
    安卓adb
    图数据库学习
    分布式架构演进
    多活架构
    异地多活
    分布式CRDT
    技术架构的战略和战术原则
    分布式金融架构课笔记
    使用jackson进行json序列化时进行敏感字段脱敏(加密)或者忽略
    读书笔记《演进式架构》
  • 原文地址:https://www.cnblogs.com/lovetl/p/12749773.html
Copyright © 2011-2022 走看看