Announcement Announcement Module
Collapse
No announcement yet.
Spring FTP download filter not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring FTP download filter not working

    Hi,

    I am writing a service to download files using Spring FTP.
    The problem is i want to download file of the currentdate only. my file pattern would be file_yymmdd.csv, where yymmdd would be changing everyday.
    I have written a filter service which implements FileListFilter<File>.
    Code:
    public class FileFilterTest implements FileListFilter<File>
    {
    
    	@Override
    	public List<File> filterFiles(File[] files)
    	{
    		System.out.println("in FILTER FILES");
    		List<File> currentFileList = new LinkedList<File>();
    		
    		for(File file:files)
    		{
    			/*if(file.getName().equals("files_120928.csv"));
    			currentFileList.add(file);*/
    			System.out.println("File --- "+file);
    		}
    		
    		return currentFileList;
    	}
    
    
    }

    My config is as follows:

    Code:
    	<int:channel id="ftpChannel">
    		<int:queue />
    	</int:channel>
    
    	<int-ftp:inbound-channel-adapter id="ftpinbound"
    		session-factory="ftpClientFactory" channel="ftpChannel"
    		charset="UTF-8" 
    		local-directory="/home/annuk/Downloads/inboundadapter/stores/"
    		remote-directory="/Stores/"
    		filter="fileFilter"
    		>
    		<int:poller fixed-rate="1000" />
    	</int-ftp:inbound-channel-adapter>
    
    
    	<bean id="ftpClientFactory"
    		class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    		<property name="host" value="test" />
    		<property name="port" value="21" />
    		<property name="username" value="test" />
    		<property name="password" value="testftp" />
    	</bean>
    
    	<bean id="fileFilter" class="com.ftp.filedownload.FileFilterTest" />
    My javacode is as follows:

    Code:
    public static void main(String[] args)
    	{
    		System.out.println("in download test...");
    		FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
    				"/WebContent/WEB-INF/config/spring/ftp/ftp-download.xml");
    
    		PollableChannel ftpChannel = context.getBean("ftpChannel", PollableChannel.class);
    		System.out.println("ftpChannel " + ftpChannel);
    		Message<List<?>> fileList = (Message<List<?>>) ftpChannel.receive();
    
    		System.out.println("FileList is " + fileList);
    
    		System.exit(0);
    
    	}
    The inbound adapter does not take the filter into consideration and downloads every file from the FTP. Where am I going wrong.
    Is there any way by which we can close the FTP connection after the files are downloaded?

    Please help.

    Thanks in advance.
    Regards,
    Annuk

  • #2
    There are couple of problems in your code.
    1. Your implementation of the FileListFilter assumes that the incoming object is java.io.File, however it is not. Its org.apache.commons.net.ftp.FTPFile. So if your filter was picked up you would have seen a ClassCastException:
    Code:
    08:19:17,001 ERROR task-scheduler-2 handler.LoggingHandler:126 - java.lang.ClassCastException: [Lorg.apache.commons.net.ftp.FTPFile; cannot be cast to [Ljava.io.File;
    	at org.springframework.integration.ftp.config.FileFilterTest.filterFiles(FileFilterTest.java:1)
    	at. . .
    . . . so change your filter definition to look like this:
    Code:
    import org.apache.commons.net.ftp.FTPFile;
    . . .
    
    public class FileFilterTest implements FileListFilter<FTPFile> {
    
    	public List<FTPFile> filterFiles(FTPFile[] files) {. . .}
    }
    If you didn't see the exception it means that the poller did not receive any Messages to be filtered. The most likely cause is miss-configuration of the 'remote-directory' attribute, basically if it points to non-existing directory. I think removing forward slash from /Stores/ would do the trick since its relative to your home directory.

    Comment


    • #3
      Hey Oleg,

      Thanks for your reply.
      Just changed the file from java.io.file to org.apache.commons.net.ftp.FTPFile and the filter worked.

      Regards,
      Annuk

      Comment

      Working...
      X