简体   繁体   English

java.lang.ClassCastException:org.primefaces.model.DefaultUploadedFile无法转换为java.io.File

[英]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.

相关问题 Drools的运行时异常(java.lang.ClassCastException:org.drools.io.impl.ClassPathResource无法强制转换为org.drools.io.InternalResource) - Runtime Exception with Drools(java.lang.ClassCastException: org.drools.io.impl.ClassPathResource cannot be cast to org.drools.io.InternalResource) java.lang.ClassCastException: org.springframework.security.core.userdetails.User 无法转换为 model.User - java.lang.ClassCastException: org.springframework.security.core.userdetails.User cannot be cast to model.User java.lang.ClassCastException:org.apache.hadoop.io.LongWritable无法强制转换为org.apache.hadoop.hbase.io.ImmutableBytesWritable - java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.hbase.io.ImmutableBytesWritable Hadoop:java.lang.ClassCastException:org.apache.hadoop.io.LongWritable无法强制转换为org.apache.hadoop.io.Text - Hadoop : java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.Text java.lang.ClassCastException:无法转换为RssLargeViewHolder - java.lang.ClassCastException: cannot be cast to RssLargeViewHolder java.lang.ClassCastException:MainActivity无法强制转换? - java.lang.ClassCastException: MainActivity cannot be cast? java.lang.ClassCastException,DeepNodeListImpl无法强制转换 - java.lang.ClassCastException, DeepNodeListImpl cannot be cast java.lang.ClassCastException:无法将java.lang.String强制转换为org.myapp.ui.MyClass - java.lang.ClassCastException: java.lang.String cannot be cast to org.myapp.ui.MyClass java.lang.ClassCastException:无法将java.lang.Boolean强制转换为org.apache.pig.data.Tuple - java.lang.ClassCastException: java.lang.Boolean cannot be cast to org.apache.pig.data.Tuple java.lang.ClassCastException:org.apache.camel.builder.ValueBuilder无法转换为java.lang.String - java.lang.ClassCastException: org.apache.camel.builder.ValueBuilder cannot be cast to java.lang.String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM