I’d like to upload a few files to a HTTP server. Basically what I need is some sort of a POST request to the server with a few parameters and the files. I’ve seen examples of just uploading files, but didn’t find how to also pass additional parameters.
What’s the simplest and free solution of doing this? Does anyone have any file upload examples that I could study? I’ve been googling for a few hours, but (maybe it’s just one of those days) couldn’t find exactly what I needed. The best solution would be something that doesn’t involve any third party classes or libraries.
You’d normally use
java.net.URLConnectionto fire HTTP requests. You’d also normally usemultipart/form-dataencoding for mixed POST content (binary and character data). Click the link, it contains information and an example how to compose amultipart/form-datarequest body. The specification is in more detail described in RFC2388.Here’s a kickoff example:
This code is less verbose when you use a 3rd party library like Apache Commons HttpComponents Client.
The Apache Commons FileUpload as some incorrectly suggest here is only of interest in the server side. You can’t use and don’t need it at the client side.
See also