I'm developing a J2ME client that must upload a file to a Servlet using HTTP.
The servlet part is covered using Apache Commons FileUpload
protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
ServletFileUpload upload = new ServletFileUpload();
upload.setSizeMax(1000000);
File fileItems = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
File file = new File("\files\\"+item.getName());
item.write(file);
}
}
Commons Upload seems to be able to upload only multipart file, but no application/octect-stream.
But for the client side there are no Multipart classes, neither, in this case, is possible to use any HttpClient library.
Other option could be to use HTTP Chunk upload, but I haven't found a clear example of how this could be implemented, specially on the servlet side.
My choices are: - Implement a servlet for http chunk upload - Implement a raw client for http multipart creation
I don't know how to implement none of the above options. Any suggestion?
Following code can be used to upload file with HTTP Client 4.x (Above answer uses MultipartEntity which is deprecated now)
You will need recent versions of the following Apache libraries: httpclient, httpcore, httpmime.
getClient()
can be replaced withHttpClients.createDefault()
.Thanks for all the code Ive sniped... Here is some back.
Sending files over HTTP is supposed to take place using
multipart/form-data
encoding. Your servlet part is fine as it already uses Apache Commons FileUpload to parse amultipart/form-data
request.Your client part, however, is apparently not fine as you're seemingly writing the file content raw to the request body. You need to ensure that your client sends a proper
multipart/form-data
request. How exactly to do it depends on the API you're using to send the HTTP request. If it's plain vanillajava.net.URLConnection
, then you can find a concrete example somewhere near the bottom of this answer. If you're using Apache HttpComponents Client for this, then here's a concrete example:Unrelated to the concrete problem, there's a bug in your server side code:
This will potentially overwrite any previously uploaded file with the same name. I'd suggest to use
File#createTempFile()
for this instead.Without entering to gory details your code looks fine.
Now you need the server side. I'd recommend you to use Jakarta FileUpload, so you do not have to implement anything. Just deploy and configure.