我正在尝试制作一个允许我通过 ssh 并执行某些命令的应用程序。
使用 JSch,我编写了一个 AsyncTask 来处理该过程。
问题是我无法访问用于 RSA 登录的 .pub 文件,因为错误 java.io.FileNotFoundException:/storage/emulated/0/MyFolder/id_rsa.pub: open failed: EACCES (Permission returned)
.在我的 Nexus 6 上,存在“MyFolder”文件夹以及 .pub 文件。权限与下载中的其他文件相同。
SomeAsyncTask.java
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.File;
public class SomeAsyncTask extends AsyncTask<String, String, String> {
public static final String TAG = "SomeAsyncTask";
@Override
protected String doInBackground(String... params) {
Log.d(TAG, params[0]);
this.submit(params[0]);
return null;
}
private void submit(String command) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession("user", "ip", 22);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/", "id_rsa.pub");
jsch.addIdentity(file.toString());
session.connect();
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whatever.whate">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
我相信我拥有所有权限并且文件夹/文件存在。 可能是什么原因造成的?
最佳答案
Permission error when getting file for use with JSch
永远记住,在 Android Marshmallow 设备上运行时,权限系统的工作方式已发生变化。
http://developer.android.com/training/permissions/requesting.html
您需要确保正确处理运行时权限,如果没有,请确保通过设置应用程序允许设备上的所有权限。
关于java - 获取用于 JSch 的文件时出现权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33840761/