• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help in Multipart Request Urgent

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic