【问题标题】:Downloading file via SFTP using JSCH (android)使用 JSCH (android) 通过 SFTP 下载文件
【发布时间】:2015-07-11 00:49:49
【问题描述】:

我正在尝试制作一个使用 SFTP 从服务器下载文件的应用。每当我运行调试器时,一切似乎都很好,没有错误。我的日志说它已经下载了指定的文件。但是,我似乎无法在我的设备中的任何地方找到应该下载的文件。有什么帮助吗?

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import java.util.List;


public class MainActivity extends Activity {

    private String user = "user";
    private String pass = "pass";
    private String host = "hostname";
    private int portNum = 22;

    private String fileName = "sample.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new AsyncTask<Void, Void, List<String>>() {
            @Override
            protected List<String> doInBackground(Void... params) {
                try {
                    Downloader(fileName);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

        }.execute();
    }

    public void Downloader(String fileName) {

        JSch jsch = new JSch();
        Session session = null;

        String knownHostsDir = "/home/user/";

        try {
            jsch.setKnownHosts(knownHostsDir);
        } catch (JSchException e) {
            e.printStackTrace();
        }

        try {

            session = jsch.getSession(user, host, portNum);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(pass);
            session.connect();

            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftpChannel = (ChannelSftp) channel;
            sftpChannel.get(fileName);

            Log.d(fileName, " has been downloaded");

            sftpChannel.exit();
            session.disconnect();

        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }

}

【问题讨论】:

    标签: java android sftp jsch download


    【解决方案1】:
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    sftpChannel.get(fileName);
    Log.d(fileName, " has been downloaded");
    

    single-argument version of ChannelSftp.get() 不会将远程文件写入本地文件。它返回一个输入流。您应该从 InputStream 中读取远程文件的内容,例如:

    try (FileOutputStream out = new FileOutputStream("/some/file")) {
        try (InputStream in = sftpChannel.get(fileName)) {
            // read from in, write to out
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
        }
    }
    

    或者,other versions of the get() method 会为您将远程内容写入本地文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多