This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
HI, I have a servlet using to upload a file from local machine to server side. My question is how I can get the file name from file field (something like request.getParameter("filefield")). I need it to check if the file exists on the same directory (on server side) or not before uploading the file. Thanks a lot and have a nice weekend. Yongping
Hope that helps. ------------------ Miftah Khan - Sun Certified Programmer for the Java 2 Platform - Sun Certified Web Component Developer for the J2EE Platform
Joined: May 15, 2001
Thanks for your response. But based on the following lines: 1. MultipartRequest multi = new MultipartRequest(request, "/directory/to/save/uploads/to", maxPostSizeinBytes); 2. String filename = multi.getParameter("filefield"); I think line 1 has already uploaded the file to server machine before I get filename to do validation. Again, thanks.
Joined: Nov 22, 2008
Ah-ha, Try this instead.. I extracted this code from MultipartRequest:
------------------ Miftah Khan - Sun Certified Programmer for the Java 2 Platform - Sun Certified Web Component Developer for the J2EE Platform
The other suggestion is to do the upload to a temporary directory, and then move the file around afterwards. This is the only way to do it, if there are *other* things you want to check besides the filename. Eg: If the user can select the directory to upload the file to, through use of a checkbox, there is no guarantee that the MultipartParser will give you the checkbox field before the file field.