开发者

Primefaces FileUpload Get File Extension

开发者 https://www.devze.com 2023-02-19 05:58 出处:网络
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 or

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消