[英]java.lang.ClassCastException: org.primefaces.model.DefaultUploadedFile cannot be cast to java.io.File
I'm using primefaces upload file component everything works fine : my problem that I need to retrieve the uploaded file : 我正在使用primefaces上传文件组件,一切正常:我需要检索上传文件的问题:
public void handleFileUpload(FileUploadEvent event) {
selectedFile=(File) event.getFile();
}
the uploaded file has DefaultUploadedFile type class but I need File class type . 上传的文件具有DefaultUploadedFile类型类,但我需要File类类型。 I try to cast it but I get cast error so i found another solution it's creating the file using the DefaultUploadedFile URI
我尝试投射它,但是出现投射错误,所以我找到了另一个解决方案,它正在使用DefaultUploadedFile URI创建文件
selectedFile=new File(Uri)
but I couldn't find a way to retrieve it from event.getFile()
但我找不到从
event.getFile()
检索它的方法
Any idea will be appreciated 任何想法将不胜感激
I don't think that you can cast ist to java.io.File
. 我认为您不能将ist强制转换为
java.io.File
。 The instance returned by the getFile()
method is of type UploadedFile
(which is completely unrelated to java.io.File). getFile()
方法返回的实例的类型为UploadedFile
(与java.io.File完全无关)。 You could however use the getInputStream()
method in order to retrieve an InputStream
and read the file like so: 但是,您可以使用
getInputStream()
方法来检索InputStream
并按如下方式读取文件:
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. UploadedFile接口提供了其他有用的方法来检查上传的文件。 For more information see: http://www.dzeek.net/javadoc/primefacesdocs/org/primefaces/model/UploadedFile.html
有关更多信息,请参见: http : //www.dzeek.net/javadoc/primefacesdocs/org/primefaces/model/UploadedFile.html
In your method get InputStream
from UploadedFile
. 在您的方法中,从
UploadedFile
获取InputStream
。 And from InputStream
it is easy way to have File
. 从
InputStream
可以很容易地拥有File
。 Example ( How to convert InputStream to File in Java ) 示例( 如何在Java中将InputStream转换为File )
public void handleFileUpload(FileUploadEvent event) {
UploadedFile selectedFile = event.getFile();
InputStream inputStream = selectedFile.getInputstream();
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.