I'm using <t:inputFileUpload> from MyFaces to choose a file from a directory but, from the ManagedBean, when I try to get the full path of the file chosen, it returns null.

Look at the code:

InsereDocumento.xhtml

            Choose a PDF file:
     <br /> <t:inputFileUpload value="#{inserirBean.uploadedFile}" /> <br />
          <h:commandButton value="inserir" action="#{inserirBean.submit}" />

inserirBean.java - I only put here the code that matters...

 private UploadedFile uploadedFile;

public void submit() throws IOException {

      String fullPath = FilenameUtils.getFullPath(uploadedFile.getName());
      System.out.println("Full Path: " + fullPath);
    }

   public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;

    }

It prints anything, the FilenameUtils.getFullPath(uploadedFile.getName()) returns null and the fullpath string variable is null.

What's the problem here?

有帮助吗?

解决方案

How exactly is the client's disk file system path of a file upload ever relevant to the server? Imagine that you got a String with value "c:\path\to\password.txt", how would you as being a web server running at a physically different machine ever grab its contents?

Only browsers exposing a security bug like MSIE incorrectly sends the full file path along with the file content. Other (read: the sane) browsers don't send this information, they only sends the file name along with the file content. Even then, this information is useless. You can't use new File() on it for the very simple reason because that file isn't on the server's disk file system, but on the client's disk file system.

You should be interested in the actual file content, not in the file path and less in the file name.

InputStream content = uploadedFile.getInputStream();

Just write it to a FileOutputStream on server's disk file system and then use that File instead.

See also:

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top