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.
Here is how you would do it with Apache HttpClient (this solution is for those who don't mind using a 3rd party library):
click link get example file upload clint java with apache HttpComponents
http://hc.apache.org/httpcomponents-client-ga/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
and library downalod link
https://hc.apache.org/downloads.cgi
use 4.5.3.zip it's working fine in my code
and my working code..
And use
<form enctype="multipart/form-data">
and use<input type="file">
in the htmlYou'd normally use
java.net.URLConnection
to fire HTTP requests. You'd also normally usemultipart/form-data
encoding for mixed POST content (binary and character data). Click the link, it contains information and an example how to compose amultipart/form-data
request 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
It could depend on your framework. (for each of them could exist an easier solution).
But to answer your question: there are a lot of external libraries for this functionality. Look here how to use apache commons fileupload.