【问题标题】:java.io.FileNotFoundException: /key.p12: open failed: ENOENT (No such file or directory)java.io.FileNotFoundException:/key.p12:打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2015-03-30 07:28:59
【问题描述】:

我正在开发我在 android 应用程序中使用 bigquery 的应用程序。我在 API 控制台上创建了一个项目。

我已将 p12 文件放在应用程序的 assets 文件夹中,并且代码中的路径正确,但我收到错误 java.io.FileNotFoundException: /key.p12: open failed: ENOENT (No such file or directory)

请帮助我需要做些什么改变来解决它。

    try {
        credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountScopes(SCOPES)
                .setServiceAccountId("xxxxxxxxxxxxxxxx@developer.gserviceaccount.com")
                .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
                .build();
    } catch (GeneralSecurityException | IOException e1) {
        e1.printStackTrace();
    }

【问题讨论】:

  • 您是否在清单文件中获得了写入权限。
  • 看来你只是给文件命名,没有根路径。它可能会抛出FileNotFoundException
  • 您是如何从第 12 页创建 File 对象的?

标签: java android google-api google-bigquery


【解决方案1】:

这是从资产中读取文件的正确方法:

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

        credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountScopes(SCOPES)
                .setServiceAccountId("xxxxxxxxxxxxxxxx@developer.gserviceaccount.com")
                .setServiceAccountPrivateKeyFromP12File(file )
                .build();
    } catch (GeneralSecurityException | IOException e1) {
        e1.printStackTrace();
    }

从 InputStream 创建文件

private File createFileFromInputStream(InputStream inputStream) {

   try{
      File f = new File(my_file_name);
      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;
}

【讨论】:

  • createFileFromInputStream 是什么?
  • @Williams,我已经添加了。
  • 它将在哪里从输入流创建文件?
  • 当我写这篇File f = new File(key.p12.);时,你能告诉它在哪里创建文件吗?
【解决方案2】:

本教程:http://examples.javacodegeeks.com/java-basics/exceptions/java-io-filenotfoundexception-how-to-solve-file-not-found-exception/ 通过示例说明如何解决“找不到文件”异常。基本上,当您遇到上述错误时(没有这样的文件或目录);您需要确保指定的“文件:在您的情况下为 key.p12”是正确的,并且是指文件本身。
您可以尝试指定 key.p12 的整个路径并查看它的运行情况吗? 例如:(new File("c:/key.p12");)

【讨论】:

    【解决方案3】:

    您可以使用 AssetManager,还可以选择将密钥文件放在原始文件夹中并从那里获取。

    由于您使用的是android,我希望这个答案会有所帮助

    .\key.p12: open failed: ENOENT (No such file or directory)

    祝你有美好的一天.. :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多