How to get the file name for <input type="file" in jsp

31,746

First, to clear a common misunderstanding: the file path is worthless in the server side. Imagine that I am the client and I give you the file path c:/passwords.txt, how would you as being the server ever get its contents? Using java.io.File? No? That would only work if both client and server runs at the physically same machine. The only possible occurence is the local development environment.

Second, to clarify a restriction: Javascript cannot do anything with a input type="file" element due to security restrictions. If it was possible, then one could develop a website which sets the uploaded file to c:/passwords.txt and submits the form during onload. That's easy unaskingly collecting of all password files from everyone who visits the website! No?

After all, you're rather interested in the file contents. As stated in the HTML forms spec you need to set the request method to POST and the request encoding to multipart/form-data in the parent <form> element.

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

This way the file will be sent in the request body. As the standard Servlet API versions up to with 2.5 doesn't have builtin facilities to parse mulipart/form-data requests, you need to parse the request yourself. The best way is to use Apache Commons FileUpload for this. Follow the link and read both the User Guide and Frequently Asked Questions for code examples and tips&tricks. When you're already on Servlet 3.0, then you can just use the Servlet API provided HttpServletRequest#getParts() for this. You can find here an article with code examples about that.

Share:
31,746
deepthinker121
Author by

deepthinker121

Updated on April 08, 2020

Comments

  • deepthinker121
    deepthinker121 about 4 years

    I want to read the file path from html input type="file" (the entry selected in the file dialog by the user)

    <script>   
        function OpenFileDialog(form) {    
            var a = document.getElementById("inputfile").click();
            SampleForm.filePath.value = //set the path here
            document.SampleForm.submit();   
        } 
    </script>
    
    <form name="SampleForm" action="TestServlet" method="get">
        <input type="file" style="display:none;" id="inputfile"/> 
        <a href="javascript:OpenFileDialog(this.form);">Open here</a>
        <input type="hidden" name="filePath" value=""/> 
    </form>
    

    I want the path of the selected file to be read in my Servlet class How do I get the file path? Can I read it from var a? Or is there any way to directly access the file path from the input type="file" from my servlet?

  • deepthinker121
    deepthinker121 about 14 years
    Thanks BalusC for your detailed explanation.. :) I am new to this and hence did not understand the implications of accessing the file path... I guess I need to use Servlet upto version 2.5 and so I will follow the approach you have mentioned for reading the contents of the file... Thanks once again !!
  • BalusC
    BalusC about 14 years
    No, there is no restriction in servlet version. Up to version 2.5 there are no builtin ways to ease parsing uploaded files. You need to grab Apache Commons FileUpload then. But if you're already using the fresh new 3.0, then you can use builtin ways to parse uploaded files.
  • deepthinker121
    deepthinker121 about 14 years
    ok,got it.. I also want to perform the reverse operation - download a file from the server - want a file dialog to open to select the download location... how can this be done?
  • BalusC
    BalusC about 14 years
    Basically just set the Content-Disposition header to attachment. A basic servlet example can be found here: balusc.blogspot.com/2007/07/fileservlet.html