Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File field content  RSS feed

 
Yongping Wang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. I will try it.
Yongping
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!