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 [email protected] url
I am getting a HTTP 415-Unsupported Media Type Error. What is wrong ?
Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.
There are two options in front of us to resolve the issue. The first option is to change the request payload format according to what the server expects. The second option is to update the API request so that it starts supporting JSON format.
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
@POST
@Path("/simpleupload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void simpleUpload(
//@Context UriInfo ui,
@Context HttpServletRequest request
){
String fileRepository = "D:\\";
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
if (items != null) {
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (!item.isFormField() && item.getSize() > 0) {
System.out.println("File is found.");
String fileName = processFileName(item.getName());
try {
String savePath = fileRepository + fileName;
System.out.println("savePath:" + savePath);
item.write(new File(savePath));
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("getFieldName:" + item.getFieldName());
System.out.println(item.getString());
}
}
}
}
}
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With