英文:
String to FileInputSteam
问题
FileInputStream serviceAccount =
new FileInputStream("push-manager-app/src/main/resources/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://test1-use.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
上述的 Firebase
连接器代码工作正常。但在 openshift
环境中,我不能将 serviceAccountKey.json
提交到资源中,因为它在不同的环境中会有所不同。我可以从每个环境的 openshift
配置映射中获取它作为 String
。
为了运行这段代码,我需要将 String
转换为 FileInputStream
。我不确定如何做。我有一个变通方法,可以读取 String
,生成文件并使用它。但我希望使用正确的方法。我检查了 Firebase
API 的其他选项,但它甚至无法处理 InputStream
。
<details>
<summary>英文:</summary>
FileInputStream serviceAccount =
new FileInputStream("push-manager-app/src/main/resources/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://test1-use.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
The above code for `Firebase` connector is working fine. But in `openshift` environment I can't commit `serviceAccountKey.json` in resources as it will be different for different environments. I can get it as `String` from `openshift` configmap in each environment.
For running this block of code I need to convert `String` in `FileInputStream`. I'm not sure how to do it. I have one workaround where I can read `String`, generate file and use it. But I wanted to use the right way. I checked `Firebase` API for other options but it can't even handle `InputStream`.
</details>
# 答案1
**得分**: 1
String json = "xxxxxx";
InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
<details>
<summary>英文:</summary>
String json = "xxxxxx";
InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
</details>
# 答案2
**得分**: 0
```java
private static InputStream getFile() {
return FireBaseService.class.getResourceAsStream("/dataCred.json");
}
private static final String DATABASE_URL = "DATABASE_URL";
public static void initiainzeSDK() {
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(getFile()))
.setDatabaseUrl(DATABASE_URL)
.build();
FirebaseApp.initializeApp(options);
// Initialize the default app
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
make sure ur json file not wrong syntax
英文:
private static InputStream getFile() {
return FireBaseService.class.getResourceAsStream("/dataCred.json");
}
private static final String DATABASE_URL = "DATABASE_URL";
public static void initiainzeSDK() {
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(getFile()))
.setDatabaseUrl(DATABASE_URL)
.build();
FirebaseApp.initializeApp(options);
// Initialize the default app
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
make sure ur json file not wrong syntax
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论