Get full path of selected file in JSF

2019-02-20 21:57发布

I need a JSF button which allows user to browse and select a particular file, then I need my program to retrieve full path of the file.

This functionality is required when user runs the client portion from the server itself (i.e. browses to the file in the same server which hosts the web app). If the user sends request from another machine, then uploading the file is enough.

For now, I am using h:inputFile tag.

 <h:inputFile id="file" value="#{TrainingDirectoryBean.file}"></h:inputFile>

My bean : TrainingDirectoryBean has the following property

protected Part file;

But I can't get full path from it. What is the solution?

标签: jsf jsf-2.2
1条回答
够拽才男人
2楼-- · 2019-02-20 22:36

The JSF <h:inputFile> component generates a HTML <input type="file"> element which is officially only capable of sending the file content along with the sole file name — although certain browsers, particularly the one developed by some team in Redmond, also send the full file path along with the file name due to a security bug; which should thus certainly not be relied upon as it may be fixed and removed in future versions.

In HTML terms, there does not exist any element which allows the enduser to select a full file path from its local disk file system and send it in its entirety (without the associated file content) to the server. Since JSF is in the context of this question merely a HTML code producer, it isn't possible in JSF either.

Consider creating a Swing based Applet which you in turn embed the usual way in your HTML page (or JSF page, depending on the perspective how you interpret things). The Swing JFileChooser is capable of the task of selecting a local disk file system location and obtaining the full path. You can in turn send this information via URLConnection to the server (don't forget to sign the applet to avoid bothering and worrying the enduser about the security warnings it may produce).

查看更多
登录 后发表回答