.key.p12: open failed: ENOENT (No such file or directory)(.key.p12:打开失败:ENOENT(没有这样的文件或目录))
问题描述
在我的 android 应用程序中,我正在访问 Google 云存储.我已经生成了私钥 xxxxxxxkey.p12.我已将我的密钥文件放在 assets 文件夹中.但是在运行项目时它没有打开 key.p12 文件.我试过把它放在assets文件夹外面,还是没有结果.
In my android application I'm accessing the Google cloud storage . I have generated the private key xxxxxxxkey.p12 .I have put my key file in assets folder . But while running the project it is not opening the key.p12 file . I have tried putting it outside the assets folder , still no result.
httpTransport = AndroidHttp.newCompatibleTransport();
AssetManager am = getAssets();
InputStream inputStream = am.open("xxxxxxxxxxKey.p12");
File file = createFileFromInputStream(inputStream);
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com")
.setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
.setServiceAccountPrivateKeyFromP12File(file).build();
createFileFromInputStream()
private File createFileFromInputStream(InputStream inputStream) {
try {
File f = new File("download/MyKey.p12");
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
return f;
} catch (IOException e) {
// Logging exception
}
return null;
}
我在 java 项目中做过同样的事情.有什么不同,是因为 android 吗?还是文件位置的路径不正确?
I've done the same in java project.What makes the difference, is it because of android ? or the path to the file location is incorrect?
推荐答案
经过一番努力,我得到了答案,非常感谢您的支持.竖起大拇指!
After some struggle I have got my answer, Thanks a lot for your support. Thumbs up!
可以使用 AssetManager 检索文件,我们也可以将其作为原始资源
File can be retrieved using using AssetManager and also we can get it as a raw resource
使用 AssetManager
AssetManager am = getAssets();
InputStream inputStream = am.open("xxxxxxxxxxKey.p12");
File file = createFileFromInputStream(inputStream);
作为原始资源,将文件放在 res 目录下的 raw 文件夹中
As a raw resource , put the file in raw folder inside res directory
InputStream ins = getResources().openRawResource(R.raw.keyfile);
File file = createFileFromInputStream(ins);
在编写输出文件时,您必须指定密钥文件实际属于的位置,在我的情况下,我使用的是 android,我在文件夹 KeyHolder/KeyFile 内的内部存储(模拟器/设备)中创建文件
While writing the output file you have to specify where your keyfile actually belongs , in my case I'm using android, I'm creating the file inside the internal storage(emulator/device) inside folder KeyHolder/KeyFile
private File createFileFromInputStream(InputStream inputStream) {
String path = "";
File file = new File(Environment.getExternalStorageDirectory(),
"KeyHolder/KeyFile/");
if (!file.exists()) {
if (!file.mkdirs())
Log.d("KeyHolder", "Folder not created");
else
Log.d("KeyHolder", "Folder created");
} else
Log.d("KeyHolder", "Folder present");
path = file.getAbsolutePath();
try {
File f = new File(path+"/MyKey");
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
return f;
} catch (IOException e) {
// Logging exception
e.printStackTrace();
}
return null;
}
就是这样!
这篇关于.key.p12:打开失败:ENOENT(没有这样的文件或目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.key.p12:打开失败:ENOENT(没有这样的文件或目录)
基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01