java - 使用 OutputStream (android) 通过 SFTP 从远程服务器下载文件

标签 java android sftp outputstream

我正在尝试按照教程 here 中的指示使用 SFTP 从远程服务器下载文件

在输入流期间一切似乎都很好,但是当它到达输出流时,它崩溃并给我一个跟踪错误:

java.io.FileNotFoundException: /Internal Storage/Documents/sample.txt: open failed: ENOENT (No such file or directory)

Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)

有什么帮助吗?

这是我的代码:

public class MainActivity extends Activity {

    private static final String SFTPWORKINGDIR = "/path/to/file";
    private String user = "username";
    private String pass = "password";
    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;
        Channel channel = null;
        ChannelSftp sftpChannel = null;

        try {

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

            channel = session.openChannel("sftp");
            channel.connect();
            sftpChannel = (ChannelSftp) channel;
            sftpChannel.cd(SFTPWORKINGDIR); //cd to dir that contains file

            try {
                byte[] buffer = new byte[1024];
                BufferedInputStream bis = new BufferedInputStream(sftpChannel.get(fileName));
                File newFile = new File("some/file/");
                OutputStream os = new FileOutputStream(newFile); //CRASHES HERE
                BufferedOutputStream bos = new BufferedOutputStream(os);
                int readCount;
                while( (readCount = bis.read(buffer)) > 0) {
                    Log.d("Downloading", " " + fileName );
                    bos.write(buffer, 0, readCount);
                }
                bis.close();
                bos.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

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

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

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

}

最佳答案

我想我可能已经通过指定外部存储来修复它:

File path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOCUMENTS);
            File newFile = new File(path, "/" + fileName);

关于java - 使用 OutputStream (android) 通过 SFTP 从远程服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371709/

相关文章:

java - 如何使用 URL 从数据库中打印表中的多条记录

perl - SFTP文件同时上传和下载

java - Criteria API 按嵌套聚合属性排序

java - 无法设置 cookie 的值

java - 计算运行Java程序的两台计算机之间的消息延迟

Azure - Microsoft azure 是否支持 SFTP

c++ - 使用 Qt 安全上传文件

java - 从 CMD : java. lang.ClassNotFoundException : com. mysql.jdbc.Driver 执行 JAR 文件时出现运行时错误

android - 如果 ConstraintLayout 中的文本太长,TextView 会超出 Constraint

android - 首次启动时的自定义 Activity (初始化向导)