Java – how do I get the file list from the SFTP server?
I have a problem and hope to get a solution I also wrote some code, but I need to make some changes
Question: I have an SFTP server (for privacy purposes, I will provide virtual credentials) and I need to connect to
Server name: server name port: 22 user name: user name password: password
When I connect to the server, it will automatically put me in the "/ FGV" directory In this directory are several other folders I need to get the list of XML messages from the "/ FGV / US / BS /" directory and put them in list (file form) In the list, I need file directory, file name and file body I'm thinking about creating an object and putting this information 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, there is nothing in the file on the local computer
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,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 can get multiple files and put them in the object list The object should have a file directory, file name, and file body
Solution
You can use to list all files in a given directory
Vector<ChannelSftp.LsEntry> list = channelSftp.ls("*.csv"); for(ChannelSftp.LsEntry entry : list) { System.out.println(entry.getFilename()); }
Add this code later
channelSftp.cd(SFTPWORKINGDIR);
You will now get a list of file objects If you want to download all the files, the file object is entry Add this code to the 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,readCount); } bis.close(); bos.close();