This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ServletFileUpload and XML  RSS feed

 
sam wootton
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
The client has:



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

http://www.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
  • Quote
  • Report post to moderator
Hi,

Do i need to rephrase my question?

Regards, Sam
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Tim Moores
Saloon Keeper
Posts: 3902
91
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3902
91
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!