-1

Below is what I have in jsf 2.0

<tr>
    <th width="19%" align="left"><h:outputLabel value="Photo" /></th>
    <th width="2%">:</th>
    <td width="19%"><h:inputText value="#{PersonalInformationDataBean.photo}" size="80" id="photo" readonly="true"/> 
    <input type="file" name="pathPhoto" onchange="alert('you changed me...'+window.document.patentForm.pathPhoto.value);window.document.patentForm.photo.value=window.document.patentForm.pathPhoto.value"/></td>
</tr>

When I click on button Choose File and select some file, I get different path.

Chrome & Safari

I get path name as C:\fakepath\jobs_dubizzle.rtf. I always get output as C:\fakepath\FileName irrespective of any location.

Mozilla Firefox

It gives me full path perfectly.

Any idea why I am getting like this? (Different behavior across browsers.) And how to overcome? I need full path of that file so that I can use same path for storing the path in MySQL data.




Update 1

What I wanted to do is when I submit the form, I want that file to get uploaded in mysql data. Right now I would be selecting images. To upload images, I would be using code as provided here.

As you see, I would be needing full path for statement below.

File image = new File("C:/image.jpg");

That is why, I was curious to get full path of the selected file.

Any idea to get this?

4

1 回答 1

0

在 Web 应用程序中,您不希望依赖于在服务器上查找文件。您可以做的最好的事情是存储在应用程序的根目录并像这样检索:

ExternalContext external = FacesContext.getCurrentInstance().getExternalContext();
ServletContext servletContext = (ServletContext) external.getContext();
InputStream is = servletContext.getResourceAsStream("image.jpg");

理想情况下,如果您使用 JSF 并拥有 Servlet 3.0 容器,我建议您使用BalusC在他的博客中编写的文件上传,可以在这里找到。

如果您没有 Servlet 3.0,那么您可以使用Core JavaServer Faces教科书中编写的文件上传示例。您可以从作者的站点下载源代码,它位于 ch13 目录中。

当您使用上述任何组件提交表单时,您可以做任何您想做的事情,例如在操作方法中保存到数据库。

或者,如果您可以使用 PrimeFaces 之类的库,它们有一个不错的文件上传组件,您可以开箱即用。

于 2012-07-03T02:03:29.517 回答