.key.p12:打开失败:ENOENT(没有这样的文件或目录) [英] .key.p12: open failed: ENOENT (No such file or directory)

查看:29
本文介绍了.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(没有这样的文件或目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆