wood burning stoves 2.0*
The moose likes Web Services and the fly likes Web Service to upload files (using Multipart files) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Web Service to upload files (using Multipart files)" Watch "Web Service to upload files (using Multipart files)" New topic

Web Service to upload files (using Multipart files)

Sheeraz Memon

Joined: Jul 20, 2008
Posts: 3
I have to write a Java wrapper for the following REST web service:

Request URL: https://myServer:8080/myDomain/rest/myProject/run/555/log-attachment
Method: POST
Request Header:
[Accept: application/xml]
[Content-Type: multipart/form-data; boundary=XXXXXXX]

I was unclear on how and where the file boundry value would come from, that needs to be passed in to the request as a header.
Would anyone know this?

In regards to the multipart file parameter, I was a bit unclear on how the implementation would go. Would any one have any sample code for this?

This example was not too clear as it uses deprecated APIs:

Any good references/examples to multipart file uploads using Java would be greatly appreciated!


Instincts tell me that time is valued more than money....
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

What exactly are you implementing?? A wrapper class which will lie on server?? Or client of the service available on the server??

If it is wrapper on the server which will be deployed on server only then you don't have to bother about the boundary at all, it would be sent to you by client.

If you are writing a client, then you need to understand what exactly a boundary is-
Boundary is a pattern (a pattern made of different characters, don't get confused with design pattern here) which is used as terminator for multiple parts of a request, & is assumed that the parts of request will never contain that pattern. You can have your own pattern (which can even differ for every request), just make sure that it never appears inside your file. you can use ones generated by any browser or modify them as per your interest and/or need.
Sheeraz Memon

Joined: Jul 20, 2008
Posts: 3
Thanks Anurag for the response. The implementation is for the client of the service. I understand the concept of the boundary, and that it acts more like a delimiter. However, would you know of the exact steps of how to get the boundary from the browser (or how to generate a boundary)?? I could not find any example (set of steps) that has this info.

Thanks again for your time on this.
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

You can write a test servlet which prints all the headers on console for every request received & can have a sample html page having one form with one file upload input & a submit button, action of the form should be targetting to this servlet. upload the file, have all contents on the console.
Second way is ....have (or find) a web page where a file upload control exists. start tracking it with HttpFox plugin of firefox (no ad intended), upload a file & see the request headers. Copy your boundary value from there.
I agree. Here's the link: http://aspose.com/file-tools
subject: Web Service to upload files (using Multipart files)
Similar Threads
Sending multiple file from an applet to the server
Spring: Creating a servlet/controller to handle REST calls
REST security
Need some explanations about web-services basics.
posting of file between two servers not happening using multi part request