• 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

ServletFileUpload and XML

 
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks in advance for any help and/or advice.

I have a Servlet and a Java Swing client.

I want to upload a zip file to the Servlet. However, along with the zip file, i also need to send some XML.

I have 2 questions:

1)

To deal with the zip file upload i am using



How do i connect to this from my client, can i use a BufferedOutputStream to write zip file bytes?

2)

How can i send XML and binary data at the same time? XML Content-Type i have as:



and for the zip file (that needs to connect to ServletFileUpload) i have:




... is it enough to just write the XML using the same buffer, before closing?

Many thanks for your help.

Regards, sam

 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I also meant to say, that when i post from my client, i get this in the server logs:

contentType multipart
isMultipartContent false

.. appears to say it is multipart, but that also it isn't? I dont know exactly what value



reads?



Regards, Sam
 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The client has:



... which i guess is because i need a new Connection object per request?

https://coderanch.com/t/205680/sockets/java/writing-output-after-input-data

I cant seem to set the correct value that is read by conn.getContentType():



Regards, Sam
 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Do i need to rephrase my question?

Regards, Sam
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you need to clarify your requirements. It's pretty clear that you need to use an HTTP POST request, but exactly how the two files are supposed to be included is not clear. I'm pretty certain that just sending the two files one after another isn't the right thing to do.

At any rate once you get that information, I would recommend using something like Apache's HttpClient to do the upload, rather than trying to do it yourself. If it's a multipart request (which it might be) then you would have to generate a whole lot of control information in your output stream. HttpClient encapsulates all that knowledge already.
 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Many thanks for your reply, and apologies for my very dealyed response.

I got a book (jakarta commons libraries) on HTTPClient and FileUpload.

What i want to do is to upload a file and along with this file, post some XML data. The XML describes the contents of the file, so they need to be sent together.

I have got a Servlet that has the start of handling FileUploads:



The same Servlet also handles other, non multipart, HTTP requests such as:



But what want to do this time is both really (upload a file and send some XML).

Best regards, Sam
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible to upload multiple files at the same time, so you should treat the XML as a file in its own right (even if it might not exist as such on either the client or the server side). Definitely use the HttpClient library to do the upload; its documentation shows how to do that, from which point it shouldn't be too hard to work out how to upload two files.
 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Many thanks for your help and advice.

I did think of converting my "client XML post" to a file, and sending it, so (as you say), i essentially have 2 files (the XML file and my 'real' file).

I just thought this was a bit of a hack, and that i wasn't understanding using HTTPClient / FileUpload correctly.

I guess the post as a whole will be a multi-part post, so i would have to inspect each file to know whether i was dealing with my XML file or my (in this case) zip file?

Separate concern, but worth mentioning:

The book i was reading didnt mention anything about reading the number of bytes uploaded either, (if i wanted a progress bar etc).

Regards, Sam
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your book mentions "Jakarta Commons", then it's quite dated - it has been "Apache Commons" for years. I seem to remember that the FileUpload library supports a callback for implementing progress bars now; its web site should talk about that, and searching for "httpclient fileupload progress bar" also yields numerous results.
 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Thanks for your advice.

I havent had a chance to give anything a go yet, but i did find this:

http://evgeny-goldin.com/blog/uploading-files-multipart-post-apache/

Specifically, seems like he had the same problem too (wanting to upload a File and send Data to the same servlet, using the same HTTPClient instance):



Regards, Sam
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic