I'm using the primefaces fileUpload component and then I inspect the FileUploadEvent.getFile but I don'开发者_如何学Pythont see a way to reliably get the extension. Any ideas?
getFile() returns an org.primefaces.model.UploadedFile object which has a method getFileName to return the file name. Then you can get the extension from the filename.
UploadedFile tfile = event.getFile();
String str = tfile.getFileName();
String ext = str.substring(str.lastIndexOf('.'), str.length());
Another option using org.apache.commons.io.FilenameUtils
UploadedFile tfile = event.getFile();
String str = tfile.getFileName();
String prefix = FilenameUtils.getBaseName(str);
String suffix = FilenameUtils.getExtension(str);
You should do a split, eliminating the point. Remember that there may be more than one point in the title of the file, you must restore the last parameter of the split return.
UploadedFile file = e.getFile();
String [] nameFile= file.getFileName().split("[.]");
String extension= nameFile[nameFile.length-1];
精彩评论