My code for RESTful file upload :
@Path("/upload")
@POST
@Consumes("multipart/form-data")
public String post(
@FormDataParam("part") String s,
@FormDataParam("part") FormDataContentDisposition d) {
return s + ":" + d.getFileName();
}
When I try to upload a file using curl curl -X POST --form part=@file.txt url
I am getting a HTTP 415-Unsupported Media Type Error. What is wrong ?
After trying a lot of examples finaly find the realy working example on http://iambigd.blogspot.com/2011/06/java-upload-file-using-jersey.html
(need the servlet-api.jar, (apache) commons-oi.jar and (apache) commons-fileupload.jar)
This can happen due to a couple of reasons. I managed to narrow down some of them.
Your Content-Type header does not match with the one provided by the @Consumes header. Verify this with a proxy.
You managed to stumble upon a bug that was fixed in Jersey 1.4 related to the FormDataParam annotation.
You included jersey-bundle and jersey-server et all in the same binary and they are competing against each other.
You are using @FormParam instead of @FormDataParam.
Your @FormDataParam is unrecognized by the introspection API because of conflicts with jersey-multipart and other jersey jars. If one jar is of version 1.x make sure the other jars are on the same version. While debugging the jersey API code I noticed that these method annotations turn up blank (on jersey's code) if the jar versions are not uniform. All method parameters on the REST service are replaced by the body content of the POST request irrespective of which FormDataParam they are supposed to contain.
Have you tried with an Input stream ?
Like :
Works fine for me.
You may need to register the MultipartFeature as described in the Jersey documentation, chapter 8.3.1.2 Registration.
Create a class something like this:
And add the following init-param to your Jersey servlet in web.xml:
Please make sure you have mimepull.jar on the classpath