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?
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
example to use it:
if you don't want to use Apache Commons IO you can add this function to class ReverseFileNameExtensionFilter:
and change this line of function accept(File f):
with this: