Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of files from an SFTP server?

Tags:

java

sftp

jsch

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

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

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

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.

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.

like image 465
SupaHotFire Avatar asked Oct 01 '15 01:10

SupaHotFire


People also ask

Can you grep on SFTP?

How do you grep via SFTP? The grep shell command isn't built into the standard SFTP environment, so in order to use grep on a remote file, you will need to transfer the file to the local computer with SFTP and then perform a grep.

Where do SFTP get files go?

The get command in sftp allows you to download files from the sftp server. Where remote-path is the file on the server you want to download, and the optional local-path is the path you want to put the file on your machine. It defaults to your current directory.

What is ls command in SFTP?

SFTP provides options that allow users to review and manage files on both the local system and remote server. The ls command lets you list out the files and directories on the remote server. For instance: ls -l.


2 Answers

you can list all files in the given directory using

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

add this code after

channelSftp.cd(SFTPWORKINGDIR);

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();
like image 174
Kapila Ranasinghe Avatar answered Nov 04 '22 09:11

Kapila Ranasinghe


This is a way to list files of destination directory.

Vector filelist = channelSftp.ls(SFTPWORKINGDIR);
for(int i=0; i<filelist.size();i++){
     System.out.println(filelist.get(i).toString());
     // Grap and get the file by WORKING_DIR/filelist.get(i).toString();
     // Save it to your local directory with its original name. 

}

Second, while in for loop you can download all files if they are requested xml files those you need.

like image 45
Afsin Buyuksarac Avatar answered Nov 04 '22 09:11

Afsin Buyuksarac