How to upload from Android using jsch sftp?

时间秒杀一切 提交于 2019-12-10 12:23:17

问题


I'm trying to upload a picture from Android phone's DCIM folder using JSCH SFTP. This is the main part of the code:

channelSftp2.put("/storage/emulated/0/DCIM/Camera/IMG_20180118_122224.jpg","/home/pi/IMG_20180118_122224.jpg");

But unfortunately it doesn't work. The logcat does not give any specific errors. I changed /storage/emulated/0/DCIM/Camera/IMG_20180118_122224.jpg to /sdcard/DCIM/Camera/IMG_20180118_122224.jpg but nothing changed.

I'm writing the codes in Android Studio 2.2.2 and the build SDK is 24. I have added READ_EXTERNAL_STORAGE permission to the AndroidManifest.xml file.

The following is the full code snippet:

        new AsyncTask<Integer, Void, Void>(){   

            @Override
            protected Void doInBackground(Integer... params) {

                String SFTPHOST2 = "192.168.0.1";
                int SFTPPORT2 = 22;
                String SFTPUSER2 = "pi";
                String SFTPPASS2 = "raspberry";
                String SFTPWORKINGDIR2 = "/home/pi/";

                Session session2 = null;
                Channel channel2 = null;
                ChannelSftp channelSftp2 = null;

                try {
                    JSch jsch2 = new JSch();
                    session2 = jsch2.getSession(SFTPUSER2, SFTPHOST2, SFTPPORT2);
                    session2.setPassword(SFTPPASS2);
                    session2.setConfig("StrictHostKeyChecking", "no");
                    session2.setTimeout(10000);
                    while (!session2.isConnected())
                        session2.connect();

                    channel2 = session2.openChannel("sftp");
                    while (!channel2.isConnected())
                        channel2.connect();
                    channelSftp2 = (ChannelSftp) channel2;
                    channelSftp2.cd(SFTPWORKINGDIR2);

                    channelSftp2.put("/storage/emulated/0/DCIM/Camera/IMG_20180118_122224.jpg","/home/pi/IMG_20180118_122224.jpg");

                } catch (Exception ex) {
                    ex.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error Connecting", Toast.LENGTH_LONG).show();
                }
                return null;
            }

        }.execute(1);

So, what is the problem !? How can I properly upload a file from Android phone to a remote server using JSCH SFTP !?

来源:https://stackoverflow.com/questions/48317946/how-to-upload-from-android-using-jsch-sftp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!