简体   繁体   English

从p:fileUpload获取上传文件的文件路径

[英]Get file path of uploaded file from p:fileUpload

I would like to upload one file at a time, get the path of each file and add it to a list. 我想一次上传一个文件,获取每个文件的路径并将其添加到列表中。 It would later be used to save them all in a permanent directory like E:/myfile/... . 以后将用于将它们全部保存在一个永久目录中,例如E:/myfile/...

I tried the following PrimeFaces component: 我尝试了以下PrimeFaces组件:

<p:fileUpload value="#{fileUploadView.file}" mode="simple" />

However, I am unable to get the file path. 但是,我无法获取文件路径。 How can I get it? 我怎么才能得到它?

The enduser won't send you the full client side file path. 最终用户不会向您发送完整的客户端文件路径。 Even then, if it did, how would you ever get the file contents by only the path? 即使那样,如果可以的话,您将如何仅通过路径获得文件内容呢? If it were possible, it would have been a huge security breach as basically anyone on the world would be able to unaskingly scrape files from anyone else's disk through the entire Internet. 如果可能的话,这将是一个巨大的安全漏洞,因为基本上世界上任何人都可以通过整个Internet从其他人的磁盘上毫不费力地抓取文件。

Usually, only the filename is being sent and you should even not use exactly that filename to save the obtained content on disk in order to avoid files being overwritten in case (another) enduser coincidentally uploads a file with exactly same name. 通常,仅发送文件名,并且您甚至不应使用该文件名将获取的内容保存在磁盘上,以免万一(另一位)最终用户偶然上传的文件名完全相同,文件将被覆盖。 You should use it at most as metadata (eg to prefill the Save As filename in case enduser want to download it back at a later moment). 您最多应将其用作元数据(例如,预填充“ 另存为”文件名,以防最终用户稍后再下载)。

You should actually be interested in the actual file content sent to you in flavor of InputStream or byte[] , not the original client side path nor filename. 您实际上应该对以InputStreambyte[]形式发送给您的实际文件内容感兴趣,而不是原始的客户端路径或文件名。 The file content is sent only once and you should immediately read and write it to a more permanent location in the server side once the bean action method hits. 文件内容仅发送一次 ,一旦bean操作方法生效 ,您应该立即将其写入服务器端的永久位置。 Then, keep track of the (autogenerated/predefined!) filenames/paths of those saved files in some List<String> or List<File> in the view or perhaps the session scope. 然后,在视图或会话范围中的某些List<String>List<File>中,跟踪那些保存的文件的(自动生成/预定义!)文件名/路径。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM