5

I'm writing a Java desktop client which will send multiple files over the wire to a servlet using a post request. In the servlet I'm getting the input stream from the request to receive the files. The servlet will write the files to disk, one by one as they're read from the stream.

The implementation has a couple of requirements:

  • Only one HTTP request must be used to the server (so only a single stream)
  • The servlet must use a reasonable fixed amount of memory, no matter what the size of the files.

I had considered inserting markers into the stream so I know when one file ends and the next one begins. I'd then write some code to parse the stream in the servlet, and start writing the next file as appropriate.

Here's the thing... surely there's a library to do that. I've looked through apache commons and found nothing. Commons File Upload is interesting but since the upload comes from a Java app, not a browser it only solves the receiving end, not the sending.

Any ideas for a library which easily allows multiple file transfers across a single stream with fixed memory expectations even for very large files?

Thanks.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
user549335
  • 63
  • 1
  • 4

1 Answers1

7

Just use HTTP multipart/form-data encoding on the POST request body. It's described in RFC-2388 and a standard way of uploading (multiple) files by HTTP.

You can do it with just java.net.URLConnection as described in this mini-tutorial, although it would generate lot of boilerplate code. A more convenienced approach would be using Apache Commons HttpClient.

In the servlet side you can then just use Apache Commons Fileupload to process the uploaded files the usual HTTP way (or when you're already on Servlet 3.0, the HttpServletRequest#getParts(), see also this answer for examples).

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Excellent, thanks BalusC, I was missing the HttpClient part. That seems to be exactly what I need. There's a code example of it here (http://radomirml.com/2009/02/13/file-upload-with-httpcomponents-successor-of-commons-httpclient). The only problematic part I can see is tracking upload progress but I suspect I'll be able to hook into FileBody for that. Thanks again. Jon. – user549335 Dec 21 '10 at 14:39
  • @BalusC Man I really hope you see this b/c I'm lost! I would like to send multiple files and a string parameter within a request via ajax but I am not sure how to structure the send parameter to have a regular string parameter, plus multiple files as well. Could I just do ajaxObject.send(string=string&file1=file1&file2=file2); ? – gmustudent Mar 06 '13 at 08:49
  • The link does not take me to any results – gmustudent Mar 06 '13 at 12:33
  • @gmustudent: http://stackoverflow.com/search?q=user%3A157882+xmlhttprequest+formdata+servlet – BalusC Mar 06 '13 at 12:34
  • Your first tutorial is exactly what I'm doing now. I have the drag and drop and everything. Should I do ajaxObject.send(formData + params) or the other way around? Or does it not really matter? In your example you're only sending formData but I need regular params too – gmustudent Mar 06 '13 at 12:38
  • @gmustudent: just append them to `FormData`. – BalusC Mar 06 '13 at 12:39
  • I tried formData.append(params); ajaxObject.send(formData); Absolutely noting is happening when I include a file. I put print statements all over the servlet and not even the top ones get called. And when I do not include a file it gives an error that there is no multipart boundary – gmustudent Mar 06 '13 at 13:17
  • Nevermind I'll get it to work. You don a great job on here don't worry about me! – gmustudent Mar 06 '13 at 13:44