Recursive Check in SFTP Server for File

 Recursive Check in SFTP Server for File

 JAVA, SFTP, Client, Mutliple Folders

  Java    |      Anupam Chakraborty    |      Feb 12 2015 03:08 PM

 Login to Like      Login to Follow      91 Views

Requirement

At times, many of us have faced a requirement where someone asks us to go and check in a SFTP server if there are files pending, if the jobs to pick up the file are correct or not. In this case, if you have multiple folder, you would go into a SFTP Client and go to each folder to check if there is a file.

Instead of that we can create a small JAR to do this for us.

Solution

We write a JAVA code to do this for us. Note that the JAVA Code looks for a properties file in the same folder as that of the JAR file and create a file with the output. Please create a property file with name config.properties.txt and place it in the same folder as your JAR File is.

config.properties.txt

SFTPHOST=<Give your Host Here>
SFTPPORT=<Give your Port Here>
SFTPUSER=<Give your User Id Here>
SFTPPASS=<Give your Password Here>
DIRS=<Give your Directories Here separated by comma>

Please find the source code for the same.

Code

package com.sap.SFTPCheck;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;

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

public class SFTPCheck {
/**
 * @param args
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {

Date date;
DateFormat dtfFile = new SimpleDateFormat("yyyyMMdd_HHmmss");
DateFormat dtfLog = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");

int isFileFound = 0;
int numOfDir = 0;
String dir = System.getProperty("user.dir");

String SFTPHOST = null, SFTPPORTS = null, SFTPUSER = null, SFTPPASS = null, DIRS = null;
int SFTPPORT = 0;

Properties prop = new Properties();
InputStream input;

try {
input = new FileInputStream(dir + "/config.properties.txt");
prop.load(input);

SFTPHOST = prop.getProperty("SFTPHOST");
SFTPPORTS = prop.getProperty("SFTPPORT");
SFTPPORT = Integer.parseInt(SFTPPORTS);
SFTPUSER = prop.getProperty("SFTPUSER");
SFTPPASS = prop.getProperty("SFTPPASS");
DIRS = prop.getProperty("DIRS");

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}

ArrayList SFTPWORKINGDIR = new ArrayList(Arrays.asList(DIRS.split(",")));
numOfDir = SFTPWORKINGDIR.size();

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

try{

JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);

date = new Date();

java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();

PrintWriter writer = new PrintWriter(dir + "/SFTPOut_" + dtfFile.format(date) + ".txt", "UTF-8");
System.out.println("Output Log Written: " + dir + "/SFTPOut_" + dtfFile.format(date) + ".txt");

System.out.println("Tracing of Directory started at: " + dtfLog.format(date));
writer.println("Tracing of Directory started at: " + dtfLog.format(date));

//Loop at Directories.
for(int q = 0; q < numOfDir; q++){
String wrdir = (String) SFTPWORKINGDIR.get(q);
if(wrdir != null){
channelSftp = (ChannelSftp)channel;
channelSftp.cd(wrdir);
Vector filelist = channelSftp.ls(wrdir);
//Loop at File
for(int i=0; i<filelist.size();i++){
String fnm = filelist.get(i).toString();
if(fnm.endsWith("./")){

}
else{
fnm = wrdir.concat(": ").concat(fnm);
System.out.println(fnm);
writer.println(fnm);
}
}
//End Loop at File
if(filelist.size() <= 2){
String fnm = "No File Found";
fnm = wrdir.concat(": ").concat(fnm);
System.out.println(fnm);
writer.println(fnm);
}
else{
isFileFound = 1;
}
}
}
//Loop at Directory
if(isFileFound == 0){
System.out.println("Relax!!! There are no File in the SFTP Server in all the directories provided.");
writer.println("Relax!!! There are no File in the SFTP Server in all the directories provided.");
}

channel.disconnect();
session.disconnect();

date = new Date();
System.out.println("Tracing of Directory completed at: " + dtfLog.format(date));
writer.println("Tracing of Directory completed at: " + dtfLog.format(date));

writer.close();

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

Comments:

Leave a Comment:

Please login to post comment into this page.

  • Anupam Dec 14, 2018 07:10 PM

    This would save a lot of time for developers.
Please login to submit a new Blog.