当前位置: 首页 > 知识库问答 >
问题:

无法使用spring boot在firebase存储上预览上传的图像

蓟辰沛
2023-03-14

,而当我上传相同的文件从上传文件选项从firebase UI然后它是预览罚款。Pls帮助我解决这个问题。

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);
    }

共有1个答案

马新觉
2023-03-14

当您通过firebase上传文件时,UI将自动生成一个访问令牌,但对于通过Java上传的文件则没有。

您需要创建一个映射来定义一些元数据。

Map<String, String> map = new HashMap<>();
map.put("firebaseStorageDownloadTokens", imageName);

并将其传递到您的blobinfo中:

BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
                .setMetadata(map)
                .setContentType(file.getContentType())
                .build();
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();
        String imageName = fileRequest.getUploadContext() + "/" + fileRequest.getFileId();
        Map<String, String> map = new HashMap<>();
        map.put("firebaseStorageDownloadTokens", imageName);
        BlobId blobId = BlobId.of(FileConstant.bucketName, imageName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setMetadata(map).setContentType(fileRequest.getMimeType()).build();
        storage.create(blobInfo,fileByteArray);
        return fileDAO.uploadFile(fileRequest);
    }
 类似资料:
  • 我试图在Firebase存储中使用Firebase上传图像,但文件不能完全上传。它只显示图像的大小9字节,下载时无法预览。 这是我使用的代码:- 我使用的反应本地图像作物选择器。我使用Firebase,但不是反应本机Firebase。请帮助!

  • 以下是我的上传代码: 编辑:我将base64字符串复制到一个在线转换器中,看起来不错,它返回了正确的图像。看起来,数据没有问题。firebase的处理方式有问题。 编辑:我尝试显式地将类型设置为image/jpeg而不是image/jpg,如下所述:当Firestorage关闭时显示图像的正确方式,或者在Firestorage for iOS中加载预览时出错,但没有区别。

  • 我从Firebase存储中获取图像,用相机拍照,或者从图书馆中挑选一张。当其中一个完成时,我有一个类来存储,以便在需要时使用它。 现在我需要将此图像上传到Firebase存储(修改或新建,无所谓)。Firebase允许我使用以下选项之一:或,每个选项分别需要或。 我怎么能把我的,并从它获得一个或用于上传? -- 或者,为了解决这个问题,当我从Firebase存储中检索图像时,如何获取图像的? 无论

  • 我正在使用以下方法来保存信息。 我已经删除了一些用于访问数据库的代码行。 这是日志猫的描述。 E/StorageException:已发生StorageException。发生未知错误,请检查HTTP结果代码和内部异常以获取服务器响应。代码:-13000 HttpResult:0 E/AndroidRuntime:FATAL EXCEPTION:Firebase Storage-Upload-1

  • 问题内容: 嗨,我想知道在使用angularjs上传图像之前是否可以预览图像?我正在使用这个图书馆。https://github.com/danialfarid/angular- file-upload 谢谢。这是我的代码: template.html controller.js 问题答案: OdeToCode为这个东西发布了很棒的服务。因此,通过此简单的指令,您可以轻松预览甚至查看进度栏: 它适

  • 我试图上传一个图像文件,我得到了使用Imagepicker。 但上面的代码返回以下错误 VERBOSE-2:ui\u dart\u状态。cc(186)]未处理的异常:MissingPluginException(未找到方法任务的实现#通道插件上的startPutFile.flatter.io/firebase_存储)#0 MethodChannel_invokeMethod包:flatter/..