0

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?

GrizzlyManBear
  • 647
  • 8
  • 16
Luciane
  • 259
  • 4
  • 23

1 Answers1

3

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you, that was useful! But I need the file path too because I'm using a database based on graphs (an RDF store) called Allegrograph and there I have one document resource for each instance of the Document class. This document resource already have properties like title and subject, but now I need to add a property to attach the path to the document resource, so that I can search for the document on Allegrograph and show it on the screen. Is it clear? – Luciane Oct 30 '13 at 14:30
  • Still not sure why, never heard of that thing, but if you're absolutely positive that you need the full client side disk file systme path, then with Java your only resort is using Swing `JFileChooser` in flavor of an applet or webstart application which you in turn embed in your HTML webpage. – BalusC Oct 30 '13 at 14:56