简体   繁体   English

如何使用 Java 中的 Firebase Admin 将文件上传到 Firebase 存储?

[英]How to Upload file to Firebase Storage using Firebase Admin in Java?

I am trying to create a Firebase Admin Panel using Eclipse Java for my Android Studio App and I want to be able to upload content(pdf, other files, etc) to Firebase Storage so that users can download this content through my app.我正在尝试使用 Eclipse Java 为我的 Android Studio 应用程序创建一个 Firebase 管理面板,我希望能够将内容(pdf、其他文件等)上传到 Firebase 存储,以便用户可以通过我的应用程序下载此内容。 Is there any way I can achieve this?有什么办法可以做到这一点? I have tried using the code below to see if this will suit my need:我尝试使用下面的代码来查看这是否适合我的需要:

Bucket bucket = StorageClient.getInstance().bucket();

Storage storage = StorageOptions.newBuilder()
                .setCredentials(credentials)
                .build()
                .getService();


BlobId blobId = BlobId.of(bucket.getName(), "folder");
System.out.println("Blob Id:"+blobId);
BlobInfo blobInfo = 
BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
System.out.println("Blob Info:"+blobInfo);
Blob blob = storage.create(blobInfo, "Hello, Cloud 
Storage!".getBytes(UTF_8));

But..... I'm getting this NullPointerException但是.....我得到了这个 NullPointerException

java.lang.NullPointerException
    at java.base/java.lang.String.getBytes(String.java:956)at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I can't seem to get much info about this...Any help would be much appreciated!我似乎无法获得有关此的太多信息...任何帮助将不胜感激!

I do not have all needed code to be sure, but it seems that you have variable UTF_8 and it was not assigned.我没有确定所有需要的代码,但似乎您有变量UTF_8并且未分配。 I suppose this should be String value "UTF-8".我想这应该是字符串值“UTF-8”。

If you want some refence I have found something like that如果你想要一些参考,我已经找到了类似的东西

I hope it will help!我希望它会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Admin SDK Java 将 PDF 文件上传到 Firebase 存储? - How to upload PDF file to Firebase Storage using Admin SDK Java? 如何使用 firebase_admin 从 python 中的 firebase 存储中下载文件 - How to download file from firebase storage in python using firebase_admin 如何上传相册到firebase存储? - How to upload an album to firebase storage? 文件上传到firebase存储失败,java安全异常,权限被拒绝 - File upload to firebase storage fails, java security exception, permission denied 如何将图像文件上传到Firebase存储并在Flutter web app中显示 - How to upload an image file to Firebase Storage and show it in the Flutter web app 如何将pdf文件上传到firebase存储并获取flutter中的url? - How to upload pdf file to firebase storage and get the url in flutter? 如何使用 react-native 和 expo 将 m4a 音频文件上传到 Firebase 存储? - How does one upload an m4a audio file to Firebase Storage using react-native and expo? Firebase 存储 API - 使用元数据上传文件 - Firebase storage API - upload file with metada 将从那里收到的文件上传到 Firebase 存储 - Upload a file received from there into Firebase Storage 如何等待firebase存储上传图片后再使用? - How to wait for firebase storage to upload image before using it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM