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

发布于 2024-11-25 01:33:39 字数 2621 浏览 2 评论 0原文

这是我使用 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);
            }
        }
    }

}

代码的输出是:

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)

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

我可以做什么来解决这个问题?

Here is my code to read a file from the another location using 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.

What can I do to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凹づ凸ル 2024-12-02 01:33:39

FileInputStream 尝试从您的本地文件系统(该文件不存在)读取该文件,而您似乎实际上想从远程文件系统读取该文件。或者您想将其下载到本地系统然后从那里读取?

无论如何,要从远程端下载,请使用 ChannelSftp,而不是 put(用于上传)。

The FileInputStream tries to read the file from your local file 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?

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

伴我心暖 2024-12-02 01:33:39

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文