Domanda

My android app is trying to upload a file onto an SFTP web server which happens to be my university server's subdomain in which I am entitled to some section of the server space.I am using the JSch library. I have set the permissions to 777 to both the root folder and the WWW folder.Here's a screenshot:

Here's the SFTP code in my Android activity:

public class SFTPConnection extends AsyncTask<Void,Void,Void>
{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        boolean conStatus = false;
        Session session = null;
        Channel channel = null;
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");

        Log.i("Session","is"+conStatus);
        try {
            JSch ssh = new JSch();
            session = ssh.getSession("pmody", HOST_ADDRESS, 22);
            session.setPassword("667758482");
            session.setConfig(config);
            session.connect();
             conStatus = session.isConnected();
            Log.i("Session","is"+conStatus);
            channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftp = (ChannelSftp) channel;
            sftp.put("/sdcard/MyCameraApp/IMG_20140206_212035.jpg", "/");
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("Session","is"+conStatus);
        } catch (SftpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("Session","is"+conStatus);
        }
        return null;
    }

}

I get the following Logcat:

 3: Permission denied
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:594)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:475)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:365)
at com.example.locationtest.MainActivity$SFTPConnection.doInBackground(MainActivity.java:351)
at com.example.locationtest.MainActivity$SFTPConnection.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)

Also, I have given permissions in the manifest to both read and write from external storage. What could be the possible cause of permission denial?

È stato utile?

Soluzione

It is probably a path problem.

When you log in you often end up in a default directory that you don't have write access to.

If this is the case you need to cd to the correct directory where you have write permissions before putting the file, or put the file with the full path.

Altri suggerimenti

Did you allow the INTERNET permission in the manifest?

<uses-permission android:name="android.permission.INTERNET" /> 

I had the same problem with ssh returning "permission denied" and it was because the app itself wasn't allowed internet access.

Actually your code worked great. A minor edit on our part worked for us (host & password too of course).

Sharing what we did differently. We passed the list of file names as an array. Or you could do that individually.

String appFilePath = Environment.getExternalStorageDirectory().toString() + "/MyCameraApp";
ArrayList<String> files_to_upload = getFileNames(GetFiles(appFilePath));


File exportFile = new File(root, files_to_upload[0].get(iFile));  //This should be your example -> IMG_20140206_212035.jpg

boolean conStatus = false;
Session session = null;
.... your code above
//sftp.put("/sdcard/MyCameraApp/IMG_20140206_212035.jpg", "/");
sftp.put(new FileInputStream(exportFile), exportFile.getName());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top