Android :将原始资源作为文件或码头资源访问。
Android : Access raw resource as a File or a jetty Resource.
我正在 android 项目中添加 SSL 密钥库,为了通过 SSH 连接,我想将密钥库文件作为 Jetty 资源或文件(我可以将其转换为资源)进行访问。不幸的是,我是 Android 的新手,不知道如何以这种方式访问原始资源。你能帮忙的话,我会很高兴。谢谢。
代码:
import org.eclipse.jetty.util.resource.Resource;
performConnection(){
// The file is present in raw directory.
Resource keystore = Resource.newClassPathResource("raw/domain.keystore");
}
此外,正如您在屏幕截图中看到的那样,Resource class 需要多个参数。只要我可以访问密钥库,任何人都适合,因为该任务只会证明很费力。
截图:
任何帮助都会很好。谢谢你。
I am new to Android and don't know how to access raw resources in such manner
原始资源是您开发机器上的文件。它们不是设备上的文件。
看看你能不能提供一个InputStream
。如果可以,请使用 getResources().openRawResource()
在您的原始资源上获得 InputStream
。
否则,您将不得不使用 InputStream
将原始资源复制到本地文件,例如 internal storage,因此您可以使用 File
。
private Resource loadKeystoreFromAPK(int rId) throws IOException {
File baseDir = getApplication().getExternalFilesDir(null);
File path = new File(baseDir, "keystore");
// Check if it exists, if not, create it
if (!path.exists()) {
path.mkdir();
}
path = new File(path, "truststore.store");
if(!path.exists()){
path.createNewFile();
}
FileOutputStream out = new FileOutputStream(path);
InputStream in = getResources().openRawResource(rId);
IOUtils.copy(in,out);
in.close();
out.close();
return Resource.newResource(path);
}
当我在这里尝试解决在我的 android 应用程序中检索我的密钥库的问题时,我没有得到任何这些建议的工作。当我尝试之前建议的解决方案时,它无法在原始文件夹中找到密钥库。
我是这样解决的:
我在我的主文件夹 app/src/main/assets
中创建了一个名为 assets 的文件夹,我将我的密钥库文件放在那里。
然后我可以使用这些步骤来检索名为 keystore.p12
的密钥库。
KeyStore keyStore = KeyStore.getInstance("PKCS12");
AssetManager ass = context.getAssets();
keyStore.load(ass.open("keystore.p12"), keyStorePassword.toCharArray());
我花了一些时间才弄明白这一点,所以我希望这可以帮助其他人。您还需要用 try/catch 块包围它,以处理可能的异常。如果您在 activity 中执行此操作,则可以在没有上下文部分的情况下调用 getAssets()
。
我正在 android 项目中添加 SSL 密钥库,为了通过 SSH 连接,我想将密钥库文件作为 Jetty 资源或文件(我可以将其转换为资源)进行访问。不幸的是,我是 Android 的新手,不知道如何以这种方式访问原始资源。你能帮忙的话,我会很高兴。谢谢。
代码:
import org.eclipse.jetty.util.resource.Resource;
performConnection(){
// The file is present in raw directory.
Resource keystore = Resource.newClassPathResource("raw/domain.keystore");
}
此外,正如您在屏幕截图中看到的那样,Resource class 需要多个参数。只要我可以访问密钥库,任何人都适合,因为该任务只会证明很费力。
截图:
任何帮助都会很好。谢谢你。
I am new to Android and don't know how to access raw resources in such manner
原始资源是您开发机器上的文件。它们不是设备上的文件。
看看你能不能提供一个InputStream
。如果可以,请使用 getResources().openRawResource()
在您的原始资源上获得 InputStream
。
否则,您将不得不使用 InputStream
将原始资源复制到本地文件,例如 internal storage,因此您可以使用 File
。
private Resource loadKeystoreFromAPK(int rId) throws IOException {
File baseDir = getApplication().getExternalFilesDir(null);
File path = new File(baseDir, "keystore");
// Check if it exists, if not, create it
if (!path.exists()) {
path.mkdir();
}
path = new File(path, "truststore.store");
if(!path.exists()){
path.createNewFile();
}
FileOutputStream out = new FileOutputStream(path);
InputStream in = getResources().openRawResource(rId);
IOUtils.copy(in,out);
in.close();
out.close();
return Resource.newResource(path);
}
当我在这里尝试解决在我的 android 应用程序中检索我的密钥库的问题时,我没有得到任何这些建议的工作。当我尝试之前建议的解决方案时,它无法在原始文件夹中找到密钥库。
我是这样解决的:
我在我的主文件夹
app/src/main/assets
中创建了一个名为 assets 的文件夹,我将我的密钥库文件放在那里。然后我可以使用这些步骤来检索名为
keystore.p12
的密钥库。
KeyStore keyStore = KeyStore.getInstance("PKCS12");
AssetManager ass = context.getAssets();
keyStore.load(ass.open("keystore.p12"), keyStorePassword.toCharArray());
我花了一些时间才弄明白这一点,所以我希望这可以帮助其他人。您还需要用 try/catch 块包围它,以处理可能的异常。如果您在 activity 中执行此操作,则可以在没有上下文部分的情况下调用 getAssets()
。