0

I want to get full path of file name from html.

Code:

 <form action="test" method="post">
  <p>Enter Name : <input type="text" name="name"></p>
  <p>Choose file : <input type="file" name="fName"></p>
  <p><input type="submit" value="Enter"></p>
 </form>

I wrote above code in html and I retrieved file name from servlet.

 String fName = request.getParameter("fName");

but it got only file name, didn't get full path of file name. I found some site that show to get full path of file name. It's code as follow.

   <form action="test" method="post" enctype="multipart/form-data">

when using above code, name and fName are null value return.

How to get full path of file name? Please explain me! Thanks.

Sharifah
  • 361
  • 2
  • 17
  • 30
  • The last attribute of form has to be `enctypt="multipart/form-data"` not `encrype` – Sunil Kumar B M Mar 05 '12 at 04:21
  • And if you use encrypt="multipart/form-date", you cannot directly use request.getParameter("paramname") to retrieve your form info – Sunil Kumar B M Mar 05 '12 at 04:25
  • Read this and enlighten yourself: http://stackoverflow.com/questions/81180/how-to-get-the-file-path-from-html-input-form-in-firefox-3/3374408#3374408 Don't rely on the path. **Trim** it off when it's present! Or even better, just ignore it altogether and give it an unique ID/name yourself. – BalusC Mar 17 '12 at 02:50

2 Answers2

1

It depend on the browser. Some browsers send the full path but this is considered a security risk because it might tell thing about the client system, so most modern browsers just send the filename. Why do you need the full path??

MTilsted
  • 5,425
  • 9
  • 44
  • 76
  • Because I need to read the file data and check this data has in DB or not. Now, **IO Error: The system cannot find the file specified** will occur when I didn't get full path of file. – Sharifah Mar 05 '12 at 04:55
  • You can't access the file anyway. You really seems to misunderstand how file submit work in html forms. Also note that java servlets can't handle file upload(enctype="multipart/form-data") as standard. Try to look at http://commons.apache.org/fileupload/ which contains a .jar file and examples – MTilsted Mar 06 '12 at 11:35
-1

If you want get full path. Just enable the "Include local directory path when uploading the files to server". procedure to enable: Internet Options-->Security-->Custom-->Include local directory path when uploading the files to server. Your code String fName = request.getParameter("fName"); will give you total path.

thunderblaster
  • 918
  • 11
  • 27