Reputation: 3270
I'm using primefaces upload file component everything works fine : my problem that I need to retrieve the uploaded file :
public void handleFileUpload(FileUploadEvent event) {
selectedFile=(File) event.getFile();
}
the uploaded file has DefaultUploadedFile type class but I need File class type . I try to cast it but I get cast error so i found another solution it's creating the file using the DefaultUploadedFile URI
selectedFile=new File(Uri)
but I couldn't find a way to retrieve it from event.getFile()
Any idea will be appreciated
Upvotes: 0
Views: 6144
Reputation: 2322
I don't think that you can cast ist to java.io.File
. The instance returned by the getFile()
method is of type UploadedFile
(which is completely unrelated to java.io.File). You could however use the getInputStream()
method in order to retrieve an InputStream
and read the file like so:
UploadedFile file = event.getFile();
InputStream in = file.getInputStream();
// do something useful with the input stream...
The UploadedFile interface provides other useful methods to inspect the uploaded file. For more information see: http://www.dzeek.net/javadoc/primefacesdocs/org/primefaces/model/UploadedFile.html
Upvotes: 2
Reputation: 716
In your method get InputStream
from UploadedFile
. And from InputStream
it is easy way to have File
. Example (How to convert InputStream to File in Java)
public void handleFileUpload(FileUploadEvent event) {
UploadedFile selectedFile = event.getFile();
InputStream inputStream = selectedFile.getInputstream();
...
}
Upvotes: 1