使用JSch从另一个位置读取文件 [英] Read a file from the another location using JSch

查看:266
本文介绍了使用JSch从另一个位置读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

代码输出为:

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,但是在此之前,我进行了SOP以使用l列出/misc/downloads/路径中的所有文件.

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?

推荐答案

FileInputStream尝试从您的本地文件系统(不存在)中读取文件,尽管看起来您实际上想从远程文件系统中读取它.还是要将其下载到本地系统,然后从那里阅读?

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?

无论如何,要从远程下载,请使用get方法之一/ChannelSftp.html"rel =" nofollow> ChannelSftp ,而不是put(用于上传).

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

这篇关于使用JSch从另一个位置读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆