简体   繁体   English

将文件从Android应用上传到SFTP服务器

[英]uploading file from Android app to SFTP server

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. 我的android应用正在尝试将文件上传到SFTP Web服务器上,该服务器恰好是我的大学服务器的子域,在其中我有权使用服务器空间的某些部分。我正在使用JSch库。 I have set the permissions to 777 to both the root folder and the WWW folder.Here's a screenshot: 我已经将根文件夹和WWW文件夹的权限都设置为777,这是屏幕截图:

Here's the SFTP code in my Android activity: 这是我的Android活动中的SFTP代码:

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: 我得到以下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? 拒绝权限的可能原因是什么?

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. 如果是这种情况,则需要在放入文件之前将CD放入拥有写入权限的正确目录,或者将文件放入完整路径。

Did you allow the INTERNET permission in the manifest? 您是否在清单中允许INTERNET权限?

<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. ssh返回“权限被拒绝”时,我遇到了同样的问题,这是因为该应用程序本身不允许互联网访问。

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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM