Author: sebb
Date: Wed Dec 10 01:07:28 2014
New Revision: 1644277

URL: http://svn.apache.org/r1644277
Log:
Add mdtm output

Modified:
    commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java

Modified: 
commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java?rev=1644277&r1=1644276&r2=1644277&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java 
(original)
+++ commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java 
Wed Dec 10 01:07:28 2014
@@ -25,6 +25,11 @@ import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
 
 import org.apache.commons.net.PrintCommandListener;
 import org.apache.commons.net.ftp.FTP;
@@ -65,6 +70,7 @@ public final class FTPClientExample
         "\t-k secs - use keep-alive timer (setControlKeepAliveTimeout)\n" +
         "\t-l - list files using LIST (remote is used as the pathname if 
provided)\n" +
         "\t     Files are listed twice: first in raw mode, then as the 
formatted parsed data.\n" +
+        "\t-m - list file details using MLSD (remote is used as the pathname 
if provided)\n" +
         "\t-L - use lenient future dates (server dates may be up to 1 day into 
future)\n" +
         "\t-n - list file names using NLST (remote is used as the pathname if 
provided)\n" +
         "\t-p true|false|protocol[,true|false] - use FTPSClient with the 
specified protocol and/or isImplicit setting\n" +
@@ -82,7 +88,7 @@ public final class FTPClientExample
     {
         boolean storeFile = false, binaryTransfer = false, error = false, 
listFiles = false, listNames = false, hidden = false;
         boolean localActive = false, useEpsvWithIPv4 = false, feat = false, 
printHash = false;
-        boolean mlst = false, mlsd = false;
+        boolean mlst = false, mlsd = false, mdtm = false;
         boolean lenient = false;
         long keepAliveTimeout = -1;
         int controlKeepAliveReplyTimeout = -1;
@@ -144,6 +150,10 @@ public final class FTPClientExample
                 listFiles = true;
                 minParams = 3;
             }
+            else if (args[base].equals("-m")) {
+                mdtm = true;
+                minParams = 3;
+            }
             else if (args[base].equals("-L")) {
                 lenient = true;
             }
@@ -373,6 +383,14 @@ __main:
                     System.out.println(f.toFormattedString());
                 }
             }
+            else if (mdtm)
+            {
+                FTPFile f = new FTPFile();
+                f.setName(remote);
+                String stamp = ftp.getModificationTime(remote);
+                f.setTimestamp(parseGMTdate(stamp) ); // parse the returned 
string
+                System.out.println(f.toFormattedString());
+            }
             else if (mlst)
             {
                 FTPFile f = ftp.mlistFile(remote);
@@ -495,5 +513,29 @@ __main:
             }
         };
     }
+
+    private static Calendar parseGMTdate(String gmtTimeStamp) {
+        SimpleDateFormat sdf;
+        final boolean hasMillis;
+        if (gmtTimeStamp.contains(".")){
+            sdf = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
+            hasMillis = true;
+        } else {
+            sdf = new SimpleDateFormat("yyyyMMddHHmmss");
+            hasMillis = false;
+        }
+        TimeZone GMT = TimeZone.getTimeZone("GMT"); // both need to be set for 
the parse to work OK
+        sdf.setTimeZone(GMT);
+        GregorianCalendar gc = new GregorianCalendar(GMT);
+        try {
+            gc.setTime(sdf.parse(gmtTimeStamp));
+            if (!hasMillis) {
+                gc.clear(Calendar.MILLISECOND);
+            }
+        } catch (ParseException e) {
+            // TODO ??
+        }
+        return gc;
+    }
 }
 


Reply via email to