.\key.p12: 打开失败: ENOENT(没有该文件或目录)

5

在我的Android应用程序中,我正在访问Google云存储。我已经生成了私钥xxxxxxxkey.p12。我已将我的密钥文件放入资产文件夹中。但是,在运行项目时,它无法打开key.p12文件。我尝试将其放在资产文件夹外面,但仍然没有结果。

     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吗?还是文件位置的路径不正确?


你尝试过将Windows路径分隔符\替换为Unix路径分隔符/吗? - bratkartoffel
你的意思是像这样 ".//key.p12" 吗? - James Hoffman
不需要,只要像 ./key.p12 这样简单。双反斜杠只在Java中作为转义字符时才需要使用,因此您需要转义转义字符 ;) - bratkartoffel
结果是相同的,ENOENT异常。 - James Hoffman
为什么不使用AssetManager来读取文件呢?如果您不想使用AssetManager,也可以将其放在raw文件夹中。 - Christine
@Christine:现在我正在使用AssetManager获取文件,同时也作为原始资源,问题在于输出文件的写入位置。 - James Hoffman
1个回答

1

经过一些努力,我已经得到了答案,非常感谢您的支持。点赞!

可以使用AssetManager检索文件,也可以将其作为原始资源获取。

使用AssetManager

        AssetManager am = getAssets();
        InputStream inputStream = am.open("xxxxxxxxxxKey.p12");
        File file = createFileFromInputStream(inputStream);

作为一个“原始资源”,将文件放在res目录下的raw文件夹中。
        InputStream ins = getResources().openRawResource(R.raw.keyfile);
        File file = createFileFromInputStream(ins);

在编写输出文件时,您需要指定密钥文件的实际位置。在我的情况下,我正在使用Android,在内部存储器(模拟器/设备)中创建文件夹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;
    }

就是这样!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接