File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes File field content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "File field content" Watch "File field content" New topic
Author

File field content

Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Try the following O'Reilly class: http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.html
All you need to do to use this class is the following:
(a) Download and install the MultipartRequest class from http://www.servlets.com/cos/index.html
(b) add "import com.oreilly.servlet.MultipartRequest" to your servlet
(c) instantiate and use the MultipartRequest object as follows:

Hope that helps.
------------------
Miftah Khan
- Sun Certified Programmer for the Java 2 Platform
- Sun Certified Web Component Developer for the J2EE Platform
Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
Thanks a lot. I will try it.
Yongping
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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.

That's from the cos FAQ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File field content