无法使用Spring Boot在Firebase存储上预览上传的图像

时间:2020-09-03 08:02:32

标签: java spring firebase spring-boot firebase-storage

我正在尝试使用Spring Boot将文件上传到Firebase存储中,以下是我的代码,我的文件正在上传,但是我尝试从Firebase UI进行预览,因为预览未加载(请参考图片){{ 3}}

,而当我从Firebase UI的“上传文件”选项上传相同文件时,则预览正常。请协助我解决这个问题。

public FileRequest uploadImage(FileRequest fileRequest, MultipartFile file) throws IOException {
        if(file.isEmpty()){
            throw new NullPointerException("No File Found..");
        }
        byte[] fileByteArray = file.getBytes();
        ClassPathResource resource = new ClassPathResource("firebase.json");
        Storage storage = StorageOptions
                .newBuilder()
                .setCredentials(ServiceAccountCredentials
                        .fromStream(resource.getInputStream()))
                .build()
                .getService();
        BlobId blobId = BlobId.of(FileConstant.bucketName,fileRequest.getUploadContext() + "/" + fileRequest.getFileId());
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(fileRequest.getMimeType()).build();
        storage.create(blobInfo,fileByteArray);
        return fileDAO.uploadFile(fileRequest);
    }

0 个答案:

没有答案