如何从SFTP服务器获取文件列表? [英] How to get list of files from an SFTP server?

查看:177
本文介绍了如何从SFTP服务器获取文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,希望得到一个解决方案。我也写了一些代码,但需要进行一些修改。

I have a problem and hoping to get a solution. I also have written some code but it needs some modification.

问题:
我有一个SFTP服务器(出于隐私目的,我将提供虚拟凭证),我需要连接到。

Problem: I have a SFTP server (for privacy purposes I will give dummy credentials) that I need to connect to.

服务器名称:server-name
port:22
用户名:username
密码:密码

Server name: server-name port: 22 username: username password: password

当我连接到服务器时,它会自动将我放到/ FGV目录中。在这个目录里面是其他几个文件夹。我需要从/ FGV / US / BS /目录中获取xml消息列表,并将它们放在LIST(文件形式的文件)中。在列表中,我需要有文件目录,文件名和文件正文。我正在考虑创建一个对象并将此信息放在那里并创建该对象的List。

When I connect to the server, it automatically drops me in the "/FGV" directory. inside this directory are couple other folders. I need to grab a LIST of xml messages from the "/FGV/US/BS/" directory and place them in a LIST (files in the form of File). In the list, I need to have the directory of the file, file name and the file body. I was thinking of creating an object and putting this information in there and creating a List of that object.

我当前的代码创建一个连接并仅下载一个xml文件。如果有两个xml文件,那么我本地计算机中的文件没有任何内容。

My current code creates a connection and downloads only ONE xml file. If there are two xml files, then the file in my local machine has nothing as content.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

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

public class SFTPinJava {

public SFTPinJava() {
}

public static void main(String[] args) {
    String SFTPHOST = "server-name";
    int SFTPPORT = 22;
    String SFTPUSER = "username";
    String SFTPPASS = "password";
    String SFTPWORKINGDIR = "/FGV";

    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;

    try {
        JSch jsch = new JSch();
        session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(SFTPWORKINGDIR);
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = new BufferedInputStream(
                channelSftp.get("/FGV/US/BS/FGVCustomsEntryLoaderService.xml"));
        File newFile = new File(
                "C:\\workspace\\Crap\\src\\org\\raghav\\stuff\\XML_FROM_SERVER.xml");
        OutputStream os = new FileOutputStream(newFile);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        int readCount;
        //System.out.println("Getting: " + theLine);
        while ((readCount = bis.read(buffer)) > 0) {
            //System.out.println("Writing: ");
            bos.write(buffer, 0, readCount);
        }

        while(session != null){
            System.out.println("Killing the session");
            session.disconnect();
            bis.close();
            bos.close();
            System.exit(0);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

我需要更改此代码,以便它可以获取多个文件并将它们放在对象列表中。该对象应具有文件目录,文件名和文件正文。

I need to change this code so that it would grab multiple files and puts them in a list of objects. that object should have the directory of the file, the file name and the body of the file.

有人可以帮助我吗?

先谢谢。

推荐答案

您可以使用

Vector<ChannelSftp.LsEntry> list = channelSftp.ls("*.csv");
for(ChannelSftp.LsEntry entry : list) {
     System.out.println(entry.getFilename()); 
}

channelSftp.cd(SFTPWORKINGDIR);

现在您将获得文件对象列表。如果要下载所有文件,则文件对象为entry。将此代码添加到for循环中。

now you'll get list of file objects. file object is entry.if you want to download all files . add this code inside to for loop.

byte[] buffer = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(channelSftp.get(entry.getFilename()));
File newFile = new File("C:/Users/Desktop/sftpStuff/"+entry.getFilename());
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
//System.out.println("Getting: " + theLine);
while( (readCount = bis.read(buffer)) > 0) {
  System.out.println("Writing: "+entry.getFilename() );
  bos.write(buffer, 0, readCount);
}
bis.close();
bos.close();

这篇关于如何从SFTP服务器获取文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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