java 使用 JSch 从另一个位置读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6747993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:07:21  来源:igfitidea点击:

Read a file from the another location using JSch

javasftpjsch

提问by poorani

Here is my code to read a file from the another location using JSch

这是我使用 JSch 从另一个位置读取文件的代码

import com.jcraft.jsch.*;
import java.io.BufferedReader;
import java.io.*;
import java.util.Vector;

public class SftpClient {
    public static void main(String args[]) {
        JSch jsch = new JSch();
        Session session = null;
        FileReader reader =null;
        BufferedReader buffer = null;
        try 
        {
.
            session = jsch.getSession("userName", "Ip Address");
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);  
            session.setPassword("password");                
            session.connect();              
            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftpChannel = (ChannelSftp) channel;
            System.out.println("Is connected to IP:"+channel.isConnected());
            Vector ls=sftpChannel.ls("/misc/downloads/");
            for(int i=0;i<ls.size();i++){
               System.out.println("Ls:"+ls.get(i));
            }
            sftpChannel.cd("/misc/downloads/");             
            File file = new File("Text.txt");
            sftpChannel.put(new FileInputStream(file),"Text.txt");              
            reader = new FileReader(file);
            buffer = new BufferedReader(reader);                
            String getLine = "";
            while ((getLine = buffer.readLine())!=null)
            {
               System.out.println("Line: "+getLine);
            }
            sftpChannel.exit();             
            session.disconnect();
        }
        catch (JSchException e) {
            e.printStackTrace();
        }
        catch (Exception e){
            e.printStackTrace();
        } 
        finally{
            try{
               if(reader!=null)
                  reader.close();
               if(buffer!=null)
                  buffer.close();
            }
            catch(Exception e){
               System.out.println("Exception:"+e);
            }
        }
    }

}

}

Output of the code is :

代码的输出是:

Is connected to IP:true
Ls:-rw-r--r--    1 root     root          733 Jul 19 17:46 Text.txt
java.io.FileNotFoundException: Text.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:103)
    at SftpClient.main(SftpClient.java:34)

I am getting the FileNotFoundException, but before that I made SOP to list all the files in the /misc/downloads/ path using ls.

我收到了 FileNotFoundException,但在此之前我使用 ls 制作了 SOP 以列出 /misc/downloads/ 路径中的所有文件。

What can I do to solve this problem?

我能做些什么来解决这个问题?

回答by Pa?lo Ebermann

The FileInputStreamtries to read the file from your localfile system (where it does not exist), while it seems that you actually want to read it from the remote file system. Or do you want to download it to the local system and then read from there?

FileInputStream试图从读取文件的本地文件系统(如果它不存在),但在我看来,你真的想从远程文件系统读取它。或者您想将其下载到本地系统,然后从那里读取?

Anyway, to download from the remote side, use one of the getmethods from ChannelSftp, not put(which is for uploading).

无论如何,从远程侧下载的使用一个get从方法ChannelSftp,而不是put(这是用于上载)。

回答by KrzyH

You can consider using Apache VFSfor reading remote files. It'simple and powerful lib.

您可以考虑使用Apache VFS来读取远程文件。它是简单而强大的库。