Search code examples
androidjschandroid-file

no such file in JSch on android


Im trying to upload a file from my service. Im doing this like this:

JSch ssh = new JSch();
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");

session = ssh.getSession("****", "*myIP*");
session.setPassword("*******");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
sftp.put(f + "/" + file, " /var/www/webimages/client/88/");
} catch (JSchException e) {
    e.printStackTrace();
} catch (SftpException e) {
    e.printStackTrace();
} finally {
    if (channel != null) {
        channel.disconnect();
    }
    if (session != null) {
        session.disconnect();
    }
}

This is my script im trying to use. The image file is found like this:

final String pathToWatch = android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";
Toast.makeText(this, "My Service Started and trying to watch " + pathToWatch, Toast.LENGTH_LONG).show();
observer = new FileObserver(pathToWatch) { // set up a file observer to watch this directory on sd card

    @Override
    public void onEvent(int event, String file) {
        if (event == FileObserver.CREATE && !file.equals(".probe")) {
            Log.d(TAG, "File created [" + pathToWatch + file + "]");

            String inputFileName =Environment.getExternalStorageDirectory().getAbsolutePath()+"/DCIM/Camera/";
            File f = new File(inputFileName);

f is in this case: /storage/emulated/0/DCIM/Camera/IMG_20150512_124943.jpg

when trying to upload i get this error log:

 2: No such file
05-12 12:49:45.311    5070-5454/com.geniusgentlemen.gp_android_app W/System.err﹕ at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
05-12 12:49:45.311    5070-5454/com.geniusgentlemen.gp_android_app W/System.err﹕ at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:594)
05-12 12:49:45.311    5070-5454/com.geniusgentlemen.gp_android_app W/System.err﹕ at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:475)
05-12 12:49:45.311    5070-5454/com.geniusgentlemen.gp_android_app W/System.err﹕ at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:365)
05-12 12:49:45.311    5070-5454/com.geniusgentlemen.gp_android_app W/System.err﹕ at com.geniusgentlemen.support_classes.service.BackgroundSpeebee$1.onEvent(BackgroundSpeebee.java:82)
05-12 12:49:45.311    5070-5454/com.geniusgentlemen.gp_android_app W/System.err﹕ at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:122)
05-12 12:49:45.311    5070-5454/com.geniusgentlemen.gp_android_app W/System.err﹕ at android.os.FileObserver$ObserverThread.observe(Native Method)
05-12 12:49:45.312    5070-5454/com.geniusgentlemen.gp_android_app W/System.err﹕ at android.os.FileObserver$ObserverThread.run(FileObserver.java:85)

What am i doing wrong i also have read and write acces in my manifest.


Solution

  • The error No such file could be misleading - I guess it's not about your source file, but about your destination directory.

    You are providing JSch with a destination of <SPACE>/var/www/webimages/client/88/" - try to trim it.

    The nice thing about open source is that it's open source: you can take a look at the real line of code where the exception is thrown. (a word of warning: I'm not sure if you want to do that...) Both the error code and the message seem to be read from a buffer, so it's pretty clear that the error is not coming from your device, but from the server.

    Looking at the beginning of the class, there is even a constant with your error code 2 defined: SSH_FX_NO_SUCH_FILE.