如何在JSF中获取上传的文件路径 [英] How to get the uploaded file path in JSF

查看:238
本文介绍了如何在JSF中获取上传的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在导入Excel文档,以在UI中读取和显示内容。我需要知道如何获得使用浏览上传的文件的路径,以读取excel文件的内容。 解决方案


我需要知道如何获取使用浏览器上传的文件的路径,以便读取excel文件的内容。

/ blockquote>

这里有一个主要的思考错误。

想象一下,我是想要上传文件的客户端,你是需要获取文件内容的服务器。我给你的路径 c:\path\to\foo.xls 作为唯一的信息。你将如何作为服务器 获取其内容?你有一个开放的TCP / IP连接到我的本地硬盘文件系统?真?是所有其他人的本地磁盘文件系统,所有的敏感信息真的很容易通过互联网访问?



这不是上传文件的工作原理。如果服务器在与客户端物理上相同的机器上运行,那么这种方法只能工作,所以你可以在该路径下使用 FileInputStream (这只会在本地开发环境中)。



您应该对客户端发送给您的唯一文件内容感兴趣。用HTML来说,你可以使用< input type =file> 。但是,在即将到来的JSF 2.2之前,没有相应的标准JSF < h:xxx> 组件。您需要查找为此提供组件的JSF组件库。在你的情况下,用RichFaces(在你的问题中标记),你可以使用< rich:fileUpload> 。目前尚不清楚您使用的是哪种RichFaces版本,但对于RF 3.3.3,您可以在 3.3.3 showcase site 和RF 4.0中的 byte [] 或者 InputStream 来表示文件内容。然后,您可以使用例如 FileOutputStream 将其存储在服务器的本地磁盘文件系统中的任何位置。你甚至可以直接把它直接提供给你正在使用的任何Excel API,它们大部分只是一个方法,使用 InputStream byte []


I'm importing Excel document for reading and displaying the content in the UI. I need to know how to get the path of the file uploaded using browse in order to read the content of the excel file.

解决方案

I need to know how to get the path of the file uploaded using browse in order to read the content of the excel file.

There's a major thinking mistake here.

Imagine that I am the client who want to upload the file and that you are the server who need to get the file's contents. I give you the path c:\path\to\foo.xls as sole information. How would you as being the server ever get its contents? Do you have an open TCP/IP connection to my local hard disk file system? Really? Is everyone else's local disk file system with all that sensitive information really so easily accessible by Internet?

That isn't how uploading files works. This would only ever work if the server runs at physically the same machine as the client, so that you can just use FileInputStream with that path (which would only occur in local development environment).

You should instead be interested in the sole file contents which the client has sent to you along with the HTTP request body. In HTML terms, you can use <input type="file"> for this. But until the upcoming JSF 2.2 there is no equivalent standard JSF <h:xxx> component for this. You need to look for JSF component libraries offering a component for this. In your case, with RichFaces (as tagged in your question), you can use <rich:fileUpload> for this. It's unclear which RichFaces version you're using, but for RF 3.3.3 you can find a complete example in the 3.3.3 showcase site and for RF 4.0 in the 4.0 showcase site.

Whatever way you choose, you should in your JSF managed bean ultimately end up with a byte[] or an InputStream representing the file contents. Then you've all the freedom to store it wherever you want on the server's local disk file system using for example FileOutputStream. You can even also just feed it directly to whatever Excel API you're using, most of them have just a method taking an InputStream or byte[].

这篇关于如何在JSF中获取上传的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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