Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Post method parameters and file attachment won't both show up

Thread view: 
ffluhart@lexmark.com - 30 Mar 2007 14:52 GMT
I'm trying to send a POST request with some parameters and a file.
I've got (basically) the following code:

     PostMethod method = new PostMethod(someURI);
     // Set parameters:
     method.addParameter("Foo", foo);
     method.addParameter("Bar", bar);

     // Add the file as a part
     FilePart filePart = new FilePart(tempFile.getName(), tempFile);
     filePart.setContentType("application/pdf");
     Part[] parts = {filePart};
     MultipartRequestEntity request = new
MultipartRequestEntity(parts, method.getParams());
     method.setRequestEntity(request);

Now, when I turn on my HTTP sniffer (I'm using Ethereal) and execute
the method, I see something with a part that is my PDF file, but I
don't see my parameters, Foo and Bar, anywhere.  My PDF is in a part
of the request that the sniffer calls "MIME multipart media
encapsulation, Type: multipart/form-data", inside that is
"Encapsulated multipart part".

If I change the order of the code like this:

     PostMethod method = new PostMethod(someURI);
     // Add the file as a part
     FilePart filePart = new FilePart(tempFile.getName(), tempFile);
     filePart.setContentType("application/pdf");
     Part[] parts = {filePart};
     MultipartRequestEntity request = new
MultipartRequestEntity(parts, method.getParams());
     method.setRequestEntity(request);

     // Set parameters:
     method.addParameter("Foo", foo);
     method.addParameter("Bar", bar);

then execute the method, I see my parameters, but no PDF portion of
the request.  The parameters are in part of the request that my
sniffer calls "Line-based text data: application/x-www-form-
urlencoded".

I don't know what I need to do in order to get my parameters AND my
file to go through.

I hope this information is helpful to someone who can help me.  I'm
not very experienced with making HTTP calls from within Java, and all
this multipart stuff is completely new to me.

Also, the receiver of my POST method is expecting to see something
like this:

     --_NextPart_000_0002_01C3E1CC.3BB37320
     Content-transfer-encoding: binary
     Foo=ABC&Bar=123

     --_NextPart_000_0002_01C3E1CC.3BB37320
     Content-transfer-encoding: binary
     Content-disposition: form-data; name="my.pdf"; filename="my.pdf"
     Content-type: application/pdf; charset=ISO-8859-1

Any help is appreciated.
Lew - 30 Mar 2007 14:58 GMT
> I'm trying to send a POST request with some parameters and a file.
> I've got (basically) the following code:
[quoted text clipped - 57 lines]
>       Content-disposition: form-data; name="my.pdf"; filename="my.pdf"
>       Content-type: application/pdf; charset=ISO-8859-1

There are different ways data can be posted, as you saw:
"application/x-www-form-urlencoded" which gives you the ability to call
request.getParameter(), and "multipart/form-data" which wraps the parameters
in deeper structures.

If you use Apache Commons fileUpload,
<http://jakarta.apache.org/commons/fileupload/using.html>
(which requires Commons I/O, to which they link), it's easy. If you receive a
post of type "multipart/form-data" you use the fileUpload structures to access
your parameters; if you receive "application/x-www-form-urlencoded" then you
call request.getParameter().

-- Lew
ffluhart@lexmark.com - 30 Mar 2007 15:29 GMT
Thanks for the quick response.  I'm not sure if FileUpload is right
for the system I'm calling, but I'll do more reading on it.

Thanks again.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.