open failed: EACCES (Permission denied) from IoBri

2019-08-07 08:44发布

问题:

I am trying to download a file through FTP. Basically I am using two tabs fragment of an activity. RemoteTabFragment and LocalTabFragment.

In LocalTabFragment, I am listing file in my Download directory. While in RemoteTabFragment, I am listing remote files and there is option to download remote file to local download directory.

For FTP operations I am using AsyncTask.Here is doInBackground function of AsyncTask:

protected FTPClient doInBackground(Void... args) {
    Log.d("siteobj", site.toString());

    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(site.get("host"));
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        ftpClient.login(site.get("login_name"), site.get("password"));

        performTask(ftpClient);

        String reply = ftpClient.getReplyString();
        if (ftpClient.getReplyString().contains("250")) {
            message = "Files loaded";
        } else {
            message = reply;
        }


    } catch (Exception e) {
        exception = e;
    }

    return ftpClient;
}

And here is performTask method used in doInBackground() method.Here I am only pasting download code in performTask method.

private FTPClient performTask (FTPClient ftpClient) {
    if(path == null) {
        path = "/";
    }
    try {
        switch (task) {
            case "download":
                ftpClient.changeWorkingDirectory(path);
                String filename = args.getString("filename");
                String localPath = args.getString("localPath");

                File localDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsoluteFile(), filename);
                //File file = new File(localDir, filename);
                Log.d("localDir", localDir.getPath());
                OutputStream outputStream = new FileOutputStream(localDir);
                Log.d("output stream", outputStream.toString());
                Boolean success = ftpClient.retrieveFile (filename, outputStream);

                workingDirectory = ftpClient.printWorkingDirectory();
                Log.d("working directory", workingDirectory);
                break;
            default:
                files = ftpClient.listFiles("/");
        }
    } catch (IOException ex){
        ex.printStackTrace();
        //Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
    }

    return ftpClient;
}

So when executing this statement: OutputStream outputStream = new FileOutputStream(localDir); It give error, actually exception and following is exact Stack Trace of exception.

    12-21 04:13:29.132     412-2013/com.hafiz.ftp W/System.err﹕ java.io.FileNotFoundException: /mnt/sdcard/Download/5021.shtml: open failed: EACCES (Permission denied)
12-21 04:13:29.152     412-2013/com.hafiz.ftp W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:406)
12-21 04:13:29.172     412-2013/com.hafiz.ftp W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
12-21 04:13:29.172     412-2013/com.hafiz.ftp W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
12-21 04:13:29.192     412-2013/com.hafiz.ftp W/System.err﹕ at com.hafiz.ftp.FtpTask.performTask(FTPTask.java:119)
12-21 04:13:29.192     412-2013/com.hafiz.ftp W/System.err﹕ at com.hafiz.ftp.FtpTask.doInBackground(FTPTask.java:72)
12-21 04:13:29.202     412-2013/com.hafiz.ftp W/System.err﹕ at com.hafiz.ftp.FtpTask.doInBackground(FTPTask.java:26)
12-21 04:13:29.212     412-2013/com.hafiz.ftp W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-21 04:13:29.223     412-2013/com.hafiz.ftp W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-21 04:13:29.232     412-2013/com.hafiz.ftp W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-21 04:13:29.242     412-2013/com.hafiz.ftp W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-21 04:13:29.242     412-2013/com.hafiz.ftp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-21 04:13:29.252     412-2013/com.hafiz.ftp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-21 04:13:29.262     412-2013/com.hafiz.ftp W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
12-21 04:13:29.272     412-2013/com.hafiz.ftp W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
12-21 04:13:29.322     412-2013/com.hafiz.ftp W/System.err﹕ at libcore.io.Posix.open(Native Method)
12-21 04:13:29.612      412-418/com.hafiz.ftp D/dalvikvm﹕ GC_CONCURRENT freed 2295K, 21% free 9538K/11975K, paused 12ms+28ms
12-21 04:13:29.712     412-2013/com.hafiz.ftp W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-21 04:13:30.002     412-2013/com.hafiz.ftp W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:390)
12-21 04:13:30.172     412-2013/com.hafiz.ftp W/System.err﹕ ... 12 more

So can you guys tell what can be reason for that? Is that because I am using OutputStream outputStream = new FileOutputStream(localDir); in doInBackground() method? I tried to create FileOutputStream outside of AsyncTask and sent in a public variable of class. But still I faced same problem. Also if I need to use this method outside of AsyncTask then let me know how can i use that while my purpose is to download file from FTP and upload there.

My permissions and settings in android manifest file are:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hafiz.ftp" >

    <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"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:debuggable="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TabActivity"
            android:label="@string/title_activity_tab" >
        </activity>
    </application>

</manifest>

Please let me know what you think i am doing wrong which is causing that exception. Let me know if you know better way to accomplish same. Also feel free to ask, if you think something is missing that I need to tell to get better answer.

Please note that I found many similar questions but many of them have differet answers so I want to know what I am doing wrong by asking this question. So please dont just redirect by point to some other solution but also tell here.

回答1:

Here is an error:

    <uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>

You need lowercase android.permission.