Friday, March 27, 2015

Learnings on FTPClient class of Apache

Exploration is very good for a person but sometimes there wouldn't be a proper location where you get what you need. This was one of those instances in my exploration phase and hence I would like to share the learnings of FTPClient class.

Problem Statement : ftpClientObject.getTimestamp() is not returning time in seconds field.

Explanation :

My code is something like below,

private static void getFTPFileProperties(FTPClient client,
            String ftpLocation, String pattern) throws IOException {
    FTPFile[] fileList=null;
    fileList = client.listFiles();
    for(int i=0;i<fileList.length;i++)
    {
        FTPFile file= fileList[0];
        Calendar cal = file.getTimestamp();
        DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(dateFormater.format(cal.getTime()));
    }
}
From the above, variable cal is should ideally retrieve timestamp which will be having even seconds field. But somehow I am able to view the date part and only the hours part. The timestamp is carrying 00 in minutes and also in seconds.

Myself being helpless posted this question in stackoverflow and thankfully Martin has caught my problem absolutely right.

Reason : listFiles() will function something like ls command of unix and thus it might not retrieve the entire timestamp part. It is suggested to use mlistDir() function in order to get the entire timestamp. But only condition the method mlistDir is defined in Apache Commons 3.0 version and I was using Commons 2.0 which was the problem. The moment I changed it to Apache Commons 3.0 it started working.

There is also a function client.getModificationTime("upload/01%20(160).jpg"); which might give the same result but this also has limitations when Commons 2.0 is used.

http://www.programdevelop.com/1506369/ would make you understand what is the problem.

Thanks to Martin who has helped me in addressing my issue and thanks to stack overflow in helping me post it.

Reference :

http://stackoverflow.com/questions/29300686/ftpclient-listfiles-is-not-returning-time-in-seconds 

No comments:

Post a Comment