如何使用Java列出仅具有txt扩展名的目录的FTP文件?

Ilavarasan jayaraman

我的ftp服务器的目录中有许多扩展名为.txt的FTP文件。

现在,我想使用Java列出扩展名为.txt的文件。

我已经尝试过.endsWith(.txt),但是它显示的编译时错误为:

无法从FTPFile转换为String

谁能帮助我实现这一目标?

以下是我的代码:

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.sql.Date;
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 void main(String args[]) throws 
SecurityException,FTPException, IOException,ConnectException, 
AddressException, MessagingException

{

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="";
Date todayDate;





     try{

         SimpleDateFormat dateformat=new SimpleDateFormat("yyyy/MM/dd");
         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());

                if(timeStamp.equalsIgnoreCase(Lastmodifieddata)  )


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

                System.out.println("File Name:"+Filename +" ||Last    
 Modified : "+Lastmodifieddata);
                 sb.append(System.lineSeparator());
                sb.append("File Name:"+Filename +" ||Last Modified : 
"+Lastmodifieddata);
                 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");
                }
     }
            System.out.println("Total Number of file :"+count);
            sb.append(System.lineSeparator());
            sb.append("Total Number of file :"+count);
            sb.append(System.lineSeparator());
          //  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("This belongs to security exception");
         sb.append(System.lineSeparator());
         sb.append("This belongs to security exception");
         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());
     }

}
}
萨蒂亚TNV

更改

if (((String) directroy[i]).endsWith("txt"))

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

编辑:

long size = directroy[i].getSize();//get file size

long getSize():获取文件大小(以字节为单位)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何仅在具有Powershell的txt文件的目录中列出具有特定扩展名的文件?

如何列出目录中除具有指定扩展名的文件以外的所有文件?

如何查找具有特定扩展名的文件,而仅排除当前目录中的某些名称?

如何从目录中仅读取具有特定扩展名的文件

如何等待仅具有固定的“ * .txt”扩展名的文件?

如何使用Python递归复制目录中具有特定扩展名的所有文件?

从资产文件夹获取仅具有.txt扩展名的文件的文件名

列出 Scala 目录下具有特定扩展名的所有文件

列出具有特定扩展名的目录中的所有文件

列出具有特定扩展名的目录和文件数量,构建特定菜单

从目录中列出具有不同扩展名的文件

列出具有特定扩展名的目录中的文件

正则表达式,以允许文件仅具有.txt或没有扩展名

使用DIR命令仅列出特定扩展名的文件

列出具有扩展名匹配条件的文件

在目录中仅获取具有给定扩展名的文件

如果使用python在该目录中找到具有特定扩展名的文件,如何移动整个目录?

在cmd中如何列出包含具有特定扩展名的文件的文件夹?

使用R列出具有指定扩展名的所有文件

如何使用Shell脚本从特定目录创建指向具有特定扩展名的所有文件的符号链接?

使用ls和grep列出具有某些扩展名的文件

从多个目录复制具有特定扩展名的文件

如何从tar文件的根目录中提取具有特定扩展名的文件?

如何仅从FTP服务器下载具有特定扩展名的文件?

Delphi仅复制具有某些扩展名的文件

仅移动具有某些扩展名的文件

Bash:如何对具有特定扩展名的文件使用“查找”,但它还会给出其先前的目录

如何创建zip归档文件,仅包含具有某些扩展名的文件

Spark:如何递归读取目录中具有不同扩展名的所有文件?