无法在springboot 2.0.2中使用GridFSFile写入磁盘

时间:2018-06-02 13:55:02

标签: spring mongodb spring-boot

尝试使用代码:

http://javasampleapproach.com/spring-framework/spring-data/springdata-mongodb-gridfstemplate-save-retrieve-delete-binary-files-image-text-files

使用springboot 1.5.13。 在此代码中检索图像使用GridFSDBFile,如下所示:

GridFSDBFile imageFile = gridOperations.findOne(new Query(Criteria.where("_id").is(imageFileId)));

以后保存在文件系统中使用:

imageFile.writeTo

Baeldung有相同的代码: http://www.baeldung.com/spring-data-mongodb-gridfs

我在springboot 2.0.2中有我的项目,这一行:

GridFSDBFile imageFile = gridOperations.findOne(new Query(Criteria.where("_id").is(imageFileId)));

抛出错误:

Type mismatch: cannot convert from GridFSFile to GridFSDBFile

所以,一旦我改为:

GridFSFile imageFile = gridOperations.findOne(new Query(Criteria.where("_id").is(imageFileId)));

没有错误,但现在我没有这个方法:

writeTo

写入磁盘。

更新1:

imageFileId = "5b0bff8b7cc45b32f43b47f4";

        GridFSFile imageFile = gridOperations.findOne(new Query(Criteria.where("_id").is(imageFileId)));
try {
            File file = new File("c:/JSA/retrieve/" + imageFile.getFilename());
            FileOutputStream streamToDownloadTo = new FileOutputStream(file);
            //This line doesn't works
            gridFSBucket.downloadToStream(imageFile.getId(), streamToDownloadTo);
            streamToDownloadTo.close();
        } catch (IOException e) {
            // handle exception
            System.out.println("error: " + e.getMessage());
        } catch (Exception e1) {
            System.out.println("error1: " + e1.getMessage());
        }

更新2:

try {
            File file = new File("c:/JSA/retrieve/" + imageFile.getFilename());
            FileOutputStream streamToDownloadTo = new FileOutputStream(file);
            System.out.println("imageFile.getId(): " + imageFile.getId());
            System.out.println("streamToDownloadTo: " + streamToDownloadTo.toString());
            gridFSBucket.downloadToStream(imageFile.getId(), streamToDownloadTo);
            streamToDownloadTo.close();
        } catch (IOException e) {
            // handle exception
            System.out.println("error: " + e.getMessage());
        } catch (Exception e1) {
            System.out.println("error1: " + e1.getMessage());
        }

控制台

imageFile.getId(): BsonObjectId{value=5b0bff8b7cc45b32f43b47f4}
streamToDownloadTo: java.io.FileOutputStream@3b20c8e2

此行抛出异常:

gridFSBucket.downloadToStream(imageFile.getId(), streamToDownloadTo);

并返回 null

解决

注入:

 @Autowired
    MongoGridFsTemplate mongoGridFsTemplate;


GridFSBucket gridFSBucket = GridFSBuckets.create(mongoGridFsTemplate.mongoDbFactory().getDb());
imageFileId = "5b0bff8b7cc45b32f43b47f4";

GridFSFile imageFile = gridOperations.findOne(new Query(Criteria.where("_id").is(new ObjectId(imageFileId))));

            try {
                File file = new File("c:/JSA/retrieve/" + imageFile.getFilename());
                FileOutputStream streamToDownloadTo = new FileOutputStream(file);
                gridFSBucket.downloadToStream(imageFile.getId(), streamToDownloadTo);
                streamToDownloadTo.close();
            } catch (IOException e) {
                // handle exception
                System.out.println("error: " + e.getMessage());
            } catch (Exception e1) {
                e1.printStackTrace();
            }

0 个答案:

没有答案