JavaFX FileChooser exclude extensions

1.4k Views Asked by At

I made an uploader application using JavaFX. (In this case, I use JavaFX 8)

I want the application to be able to upload any almost any files (audio files, media files, pdf, zip, 7z, etc). But I can't let the user to upload malicious files or harmful scripts to my server (.exe, .php, .sh should be forbidden).

The problem is, in JavaFX FileChooser I don't see any method which allow me to exclude some extensions. Instead of adding wide range of extension filter to FileChooser, I want to exclude some extensions to FileChooser.

Here's some example code:

    fc = new FileChooser();
    fc.getExtensionFilters().addAll(
        new FileChooser.ExtensionFilter("Audio files", /* some audio files extension*/),
        new FileChooser.ExtensionFilter("Video files", /* some video files extension*/),
        new FileChooser.ExtensionFilter("eBook files", /* some eBook files extension*/),
        // I want to include all media files, but exclude some extensions here
        new FileChooser.ExtensionFilter("All files", "*.*")
    );

Is there any way that I can exclude extensions in file chooser?

1

There are 1 best solutions below

2
On

For FileChooser there isn't possibility to exclude extensions you could use JFileChooser ...

You can use this class that extends FileFilter for JFileChooser not FileChooser:

I use FilenameUtils.getExtension from Apache Commons IO

 public class ReverseFileNameExtensionFilter extends FileFilter {

    private String [] fileextensions;
    private String description="";

     public ReverseFileNameExtensionFilter(String description, String... fileextension){
         this.fileextensions=fileextension;
         this.description = description;
     }

     @Override
     public String getDescription(){
       return description;
     }

     public String[] getExtensions(){
       return fileextensions;
     }

     @Override
     public boolean accept(File f){
         String [] extensions=this.getExtensions();
         String extfile = FilenameUtils.getExtension(f.getAbsolutePath());
         for (int t=0;t<extensions.length;t++){
             if (extfile.equals(extensions[t])){
                 return false;
             }  
         }
         return true;
     }

 }

example to use it:

 FileFilter filter = new ReverseFileNameExtensionFilter("File that you don't want", "exe", "dll");
 JFileChooser fileChooser = ...;
 fileChooser.addChoosableFileFilter(filter);

if you don't want to use Apache Commons IO you can add this function to class ReverseFileNameExtensionFilter:

private String getFileExtension(File file) {
    String name = file.getName();
    int lastIndexOf = name.lastIndexOf(".");
    if (lastIndexOf == -1) {
        return ""; // empty extension
    }
    return name.substring(lastIndexOf+1);
}

and change this line of function accept(File f):

String extfile = FilenameUtils.getExtension(f.getAbsolutePath());

with this:

String extfile = getFileExtension(f);