0

I have defined a SftpInboundFileSynchronizer class. I want to filter remote files which is of csv and txt files only. file extension array has *.csv and *.txt value

 SftpInboundFileSynchronizer fileSynchronizer = new 
                     SftpInboundFileSynchronizer(sftpSessionFactory());
 PathPatternParser pp = new PathPatternParser();
        PathPattern pattern = null;
 for(String fileExtension: fileExtensionArray) {
        if(pattern == null) {
            pattern = pp.parse(fileExtension);
        } else {
            pattern.combine(pp.parse(fileExtension));
        }
    }
   fileSynchronizer.setFilter(new 
        SftpSimplePatternFileListFilter(pattern.getPatternString()));

The above throws the below exception

  java.lang.IllegalArgumentException: Cannot combine patterns: *.csv and *.txt

How to fix this error?

Thanks

  • The `combine` is an `and` not an `or` and a file cannot be both *.csv and *.txt hence the failure. Instead of the `SftpSimplePatternFileListFilter` you could use the regular expression based one. – M. Deinum Jul 15 '21 at 06:30
  • @M.Deinum Can you please kindly share one link to refer? – spring dev Jul 15 '21 at 06:53
  • Use [this](https://docs.spring.io/spring-integration/api/org/springframework/integration/sftp/filters/SftpRegexPatternFileListFilter.html) class. – M. Deinum Jul 15 '21 at 07:37
  • Thanks a lot and it works....new SftpRegexPatternFileListFilter ("^.*\\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF)$"); (Another Link: https://stackoverflow.com/questions/374930/validating-file-types-by-regular-expression) - ^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF)$ and https://www.geeksforgeeks.org/how-to-validate-image-file-extension-using-regular-expression/ – spring dev Jul 15 '21 at 13:14
  • You can answer to the question and close it. Thanks a lot @M.Deinum – spring dev Jul 15 '21 at 13:15

1 Answers1

0

The PathPattern.combine method will, as the name implies, combine the patterns. Basically making it an and. A file can never end in .txt and .csv. It must be an or. You cannot achieve this, afaik, with the PathPattern.

However with a regular expression that is fairly easy to do. Spring Integration ships with a component that can handle that for you. You can use the SftpRegexPatternFileListFilter instead of the SftpSimplePatternFileListFilter.

SftpRegexPatternFileListFilter filter = new SftpRegexPatternFileListFilter ("(?i)^.*\\.(csv|txt)$");

Something with the above expression should work (I'm not a pro on regular expressions, so it might need some work).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224