aspose file tools*
The moose likes Servlets and the fly likes Help in Multipart Request Urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Help in Multipart Request Urgent" Watch "Help in Multipart Request Urgent" New topic
Author

Help in Multipart Request Urgent

Saravanan V
Greenhorn

Joined: Sep 20, 2001
Posts: 15
Hi
I am using a Multipart request for File Upload.in which i am facing this problem.
Actually from my File Upload page(jsp) i am directing to a servlet to handle that request. In that i want some variable to be passed and accessed from the servlet.
Suppose if i pass that thru Hidden variable from JSP page i cant get that in the servlet.
Saravanan.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

It's possible. Here are some code fragments. This all assumes you are using the cos package from servlets.com

Here is the form
From this you can see that I have a hidden field named 'actionTarget', a textfield named 'title' and submit buttons named 'page'. There is one file upload element called 'thumbnail'.

In my servlet, I have a few variables defined: envROOT + envUPLOAD combine to make a file upload path on my system. I import the cos packages (com.oreilly.servlet.* and com.oreilly.servlet.multipart.*). I also have defined a global MultipartRequest variable called mpRequest. And I've mapped the serEditor class to the URL 'edit' (the action of the form is this value). Here are the relevant parts of the servlet:
Hope that helps

[This message has been edited by Mike Curwen (edited September 20, 2001).]
sanjay yermalkar
Greenhorn

Joined: Sep 19, 2001
Posts: 23
There is a simple workaround for this. I tried it with Jsp and it works.
Write the hidden fields in the action itself:
<form method="post" action="edit?userName=bill" enctype="multipart/form-data">
[This message has been edited by sanjay yermalkar (edited September 20, 2001).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

In my and my colleague's experience, that work around is not reliable across browsers.

Some browsers will not pass both form data and appended parameters in the action statement.

Saravanan V
Greenhorn

Joined: Sep 20, 2001
Posts: 15
Hi
Thanx for all ur replies and the code.
Here is the problem that i have.
I want to make the directory in which i am going to upload the files to be dynamic.Thatz y i wanted to pass it as a hidden variable.In the servlet i want to get it using request.getParameter , so that i can use it for the Multipart request.But this is not working.Is this possible.Help in this.
Saravanan.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I can't think of any reason why this shouldn't work. Would it be possible for you to post your html/jsp code between <form> and </form>, and maybe we can better assist.
------------------
Miftah Khan
- Sun Certified Programmer for the Java 2 Platform
- Sun Certified Web Component Developer for the J2EE Platform
Saravanan V
Greenhorn

Joined: Sep 20, 2001
Posts: 15
Hi
Here is the code
**********
<form method=post name="multireq" action="/webapp/forJdbcServlet/uploadFiles" enctype="multipart/form-data">
Enter files to be uploaded:<br>
<input type=file name="FileName1"></input><br>
<input type=file name="FileName2"></input><br>
<input type=file name="FileName3"></input><br>
<input type=hidden name="directory" value="testing">
<input type=submit name="Upload" value="Upload"></input>
</form>
*********
In the uploadFiles servlet i want to get the directory value using request.getParameter("directory") and use it in Multipart Request.Will it be possible.
Wishes
Saravanan
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Aahh, now I understand.
In a typical POST, the request's Content-Type header by default is set to "application/x-www-form-urlencoded". This causes the form data to be URL-encoded and appended to the request URI, so that when you call request.getParameter("directory"), you get back the directory field's value.
However, in your situation, you're POST'ing using Content-Type = "multipart/form-data". This is denoted by the enctype value of your FORM tag. This is why you cannot read the hidden field value.
O'Reilly's Java Servlet Programming book has a solution to this. Use 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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I just realized that my explanation was essentially the same as Mike Curwen's. I should've read your code Mike.
------------------
Miftah Khan
- Sun Certified Programmer for the Java 2 Platform
- Sun Certified Web Component Developer for the J2EE Platform
Saravanan V
Greenhorn

Joined: Sep 20, 2001
Posts: 15
Hi Miftah
Thanx Once for ur reply.
I am using the Oreilly package as us said.
In the MultipartRequest the second argument that u pass is directory where to upload the files.So i want that before calling the MultipartRequest class.
Thatz the directory needs to be dynamic thatz what i want, by passing the directory from hidden variable.
MultipartRequest multi=new MultipartRequest(request,"directory","size").Hope u got my problem.I dont think this is much easier and would be possible.Is there any other way.
Saravanan.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You could do one of two things:

    [list] Use two MultipartRequest objects, one just to get the hidden field, and the other one to do the real work.

    [/list][list] Alternatively, you can include the directory as extra path info in the Form tag's action attribute, as follows. Just don't forget to add a servlet-mapping in your web.xml file that maps this servlet to the url-pattern "/webapp/forJdbcServlet/uploadFiles/*".

    [/list]
    ------------------
    Miftah Khan
    - Sun Certified Programmer for the Java 2 Platform
    - Sun Certified Web Component Developer for the J2EE Platform

    [This message has been edited by Miftah Khan (edited September 21, 2001).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Another option is to use the other part of the oreilly package.

There is a mutlipart parser, that you can use without having to specify an upload directory. This way you can retrieve the upload directory, and then when it parses the file, you can save to that directory.

I've never used it, as it looked more complicated. I just checked the site out to confirm what I said, and here is a snip from their FAQ (it makes the most sense, as I hadn't thought of the order not being guaranteed)...
*** Here is the quote ***
How can I select a directory for the uploaded files based on some information sent in the upload request?

Again, the protocol doesn't make this easy. In fact, there's no guarantee the browser will send the parameter information before the files. The safest approach is to create a temporary directory, upload the files into there, then after the upload move the files and/or the directory into the proper location.
** End Quote **

This seems a pretty good way to accomplish what you're after. In fact, it's how I've already coded a servlet.
[This message has been edited by Mike Curwen (edited September 21, 2001).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Oops, it just dawned on me that Option 1 in my previous post is a terrible way to solve your problem. It will effectively do two uploads. I'm not even sure if it will run without generating an exception given that it will essentially read the request inputstream twice.
Option 2 should still work though. Let me know if it does.

------------------
Miftah Khan
- Sun Certified Programmer for the Java 2 Platform
- Sun Certified Web Component Developer for the J2EE Platform
 
Don't get me started about those stupid light bulbs.
 
subject: Help in Multipart Request Urgent