如何使用java以降序打印FTP文件名?

时间:2016-02-11 03:09:41

标签: java sftp r.java-file

我必须从FTP服务器以降序获取最后三个上传的文件名,大小和上传时间。 但我只能打印所有带有名称,大小和上传时间的文件。 但它足以获得最后三个上传文件及其大小,上传时间。

我的目标是获取FTP服务器的上传详细信息,以确保文件正确上传。

任何人都可以帮助我实现这个目标吗?

package com;
import java.io.File;
import edu.vt.middleware.crypt.io.TeePrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ConnectException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.Protocol;
import com.enterprisedt.net.ftp.SecureFileTransferClient;
import java.io.File;
import java.io.FilenameFilter;
import edu.vt.middleware.crypt.io.TeePrintStream;
public class getFilesFTP
{
public static File dir=new File("D:/log_FTPCHECK");
 public static String logname="output.txt";
 public static File logfile=new File(dir,logname);
 public static StringBuffer sb=new StringBuffer();
 public static byte[] filesize;

public static void main(String args[]) throws Exception

{
    sb.append("*******************************************
************************************************
***************************************************************");
    sb.append("<p align=center><B><U>SCOPUS FILE UPLOAD CHECK AUTO   
GENERATED LOG REPORT</U></B></p>");
sb.append("************************************************
*************************************************************
*********************************************");

String host="sftp-opsbank2.elsevier.com";
String username="tho-e";
String password="Tho5540b2E";
int count=0;
File Filename;;
Date FileDate;
String invalidfilename=".";
String filetype="";
 //DateFormat dateFormat = new SimpleDateFormat("dd/mm/year");
 //String date1="";
String Lastmodifieddata="";
String Lastmodifieddata_time="";
Date todayDate;
     try{

         SimpleDateFormat dateformat=new SimpleDateFormat("yyyy/MM/dd");
         SimpleDateFormat Format_time=new SimpleDateFormat("yyyy/MM/dd   
HH:mm:ss");
     String timeStamp = 
dateformat.format(Calendar.getInstance().getTime());
        // System.out.println("I am executed2");
           // System.out.println("Todays Date :"+timeStamp );
            sb.append(System.lineSeparator());
            //sb.append("Todays Date :"+timeStamp );    
            sb.append(System.lineSeparator());
           // System.out.println("I am executed2");
            SecureFileTransferClient client=new 
SecureFileTransferClient();            
client.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);

            client.setRemoteHost(host);
            client.setUserName(username);
            client.setPassword(password);
            client.setProtocol(Protocol.SFTP);
            client.setRemotePort(22);

            client.setContentType(FTPTransferType.BINARY);
         //      sb.append(System.lineSeparator());
             System.out.println("connecting to sftp...");
          //     sb.append(System.lineSeparator());
            sb.append("connecting to sftp...");
             sb.append(System.lineSeparator());
            client.connect();
            System.out.println("SFTP Connection established 
successfully.");
            sb.append(System.lineSeparator());
            sb.append("SFTP Connection established successfully.");
            sb.append(System.lineSeparator());

            String path1="/sftp/content-providers/tho-e/data/incoming
/scopusbk";
            com.enterprisedt.net.ftp.FTPFile[] directroy = 
client.directoryList(path1);
            System.out.println("Total Number of Files Found 
:"+directroy.length);
            sb.append(System.lineSeparator());
            //sb.append("Total Number of Files Found 
:"+directroy.length);
            sb.append(System.lineSeparator());
            int x=0;

            for (int i = 0; i < directroy.length; i++) 
            {
                //System.out.println("entered in for loop");
                Filename= new File(directroy[i].getName());
                FileDate=(Date) directroy[i].lastModified();
                //Filetype=getFileExtension(Filename);
            //  System.out.println("Name:"+Filename);

  Lastmodifieddata=dateformat.format(directroy[i].
  lastModified());            
 Lastmodifieddata_time=Format_time.format(directroy[i].lastModified());

                //filesize=directroy[i].getName().getBytes();
                long size = directroy[i].size();

                if(timeStamp.equalsIgnoreCase(Lastmodifieddata))


                {   
                    if ((directroy[i]).getName().endsWith("txt"))
                    {

                System.out.println("File Name : "+Filename + " ||  
Upload Time :  "+Lastmodifieddata_time+"  || Size  : "+size+" kb");
                 sb.append(System.lineSeparator());
                sb.append("File Name : "+Filename + " ||  Upload Time :  
"+Lastmodifieddata_time+"  ||  Size  : "+size+" kb");
                 sb.append(System.lineSeparator());
                //System.out.println();


                // String path1="/sftp/suppliers/thomdi/signals
/ContentCAR";
                // com.enterprisedt.net.ftp.FTPFile[] directroy = 
client.directoryList(path1);
                count++;
                    //}
            }





                }

                else
                {
                    //System.out.println("No todays files");
                }
     }



            if(count>0)
            {
            System.out.println("Total Number of files :"+count);
            sb.append(System.lineSeparator());
            sb.append("Total Number of file :"+count);
            sb.append(System.lineSeparator());          

            }
            else
            {
                System.out.println("No Files uploaded today....!!!");
                  sb.append("No Files uploaded today....!!!");
            }


          //  PrintStream out = new PrintStream(new 
FileOutputStream("D:/output.txt"));
          //  System.setOut(out);
            if(!logfile.exists())
            {
            logfile.createNewFile();
            }



            FileUtils.writeStringToFile(logfile,sb.toString());

            FTPMailer.sendmailFTP();

            count=0;


     }
     catch(SecurityException se)
     {
         System.out.println("Security credentials mismatch 
issue...Unable to Login ");
         sb.append(System.lineSeparator());
         sb.append("Security credentials mismatch issue...Unable to 
Login ");
         sb.append(System.lineSeparator());
         se.printStackTrace();

     }
     catch(ConnectException ce)
     {
         System.out.println("Unable to Reach FTP Server..");
         System.out.println("Check the Internet Connectivity");
         sb.append(System.lineSeparator());
         sb.append("Unable to Reach FTP Server..");
         sb.append(System.lineSeparator());
         sb.append("Check the Internet Connectivity");
         sb.append(System.lineSeparator());
     }

}
}

1 个答案:

答案 0 :(得分:0)

您需要将所有相关文件信息存储在List中。循环遍历目录的所有文件后,您可以按文件的时间戳对List进行排序;第一个(或最后一个,取决于您的排序顺序)三个条目是您要查找的条目。