如何从Apache的UploadedFile的类java.io.File的获得? [英] How to get java.io.File from Apache's UploadedFile class?

查看:833
本文介绍了如何从Apache的UploadedFile的类java.io.File的获得?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何转换Apache的上传的文件 UploadedFile的类到的java.io.File 类?

How do I convert an uploaded file from apache's UploadedFile class to a java.io.File class?

推荐答案

综观文件(<一个href=\"http://myfaces.apache.org/tomahawk-project/tomahawk/apidocs/org/apache/myfaces/custom/fileupload/UploadedFile.html\"相对=nofollow> UploadedFile的 和的 文件 ),这两个类,这里​​有一个解决方案。

Looking at the documentation (UploadedFile and File) for both classes, here's one solution.

既然你可以访问的的InputStream UploadedFile的,您可以在上传文件的数据读取和其写入到一个临时位置,或您的应用程序可以管理其他位置。

Since you can access the InputStream of the UploadedFile, you can read in the data of the uploaded file and write it to a temporary location or another location that your application can manage.

// assume that you have the UploadedFile object named uploadedFile
InputStreamReader reader = new InputStreamReader(uploadedFile.getInputStream());
int partition = 1024;
int length = 0;
int position = 0;
char[] buffer = new char[partition];
FileWriter fstream = new FileWriter("out.tmp");
do{
    length = reader.read(buffer, position, partition)
    fstream.write(buffer, position, length);
}while(length > 0);
File file = new File("out.tmp");

这篇关于如何从Apache的UploadedFile的类java.io.File的获得?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆