Http 415 to file Download using jersey

My code to download a RESTful file:

@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 HTTP 415-Unsupported Media Type error message. What's wrong?

+6
source share
5 answers

Having tried many examples, we finally find a real working example at 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 servlet-api.jar, (apache) commons-oi.jar and (apache) commons-fileupload.jar)

+6
source

This can happen for several reasons. I managed to narrow some of them.

  • The Content-Type header does not match the @Consumes header. Check this with a proxy.

  • You managed to stumble upon an error that was fixed in Jersey 1.4 related to the FormDataParam annotation.

  • You have included jersey-bundle and jersey-server and all in one binary file and they compete with each other.

  • You are using @FormParam instead of @FormDataParam.

  • Your @FormDataParam is not recognized by the introspection API due to conflicts with jersey-multipart and other jersey banks. If one bank has version 1.x, make sure the other banks are in the same version. When debugging the jersey API code, I noticed that these method annotations become empty (in the jersey code) if the banner versions are not uniform. All method parameters in the REST service are replaced with the contents of the body of the POST request, regardless of which of them should contain FormDataParam.

+5
source

Please make sure you have mimepull.jar in the classpath

+1
source

You may need to register MultipartFeature as described in the Jersey documentation, section 8.3.1.2 Registration .

Create a class like this:

 /** * */ package com.verico.multipart.app; import javax.ws.rs.ApplicationPath; import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.glassfish.jersey.server.ResourceConfig; @ApplicationPath("/") public class MultiPartApp extends ResourceConfig { public MultiPartApp() { super(MultiPartFeature.class); } } 

And add the following init-param to your servlet sergeant in web.xml:

  <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.verico.multipart.app.MultiPartApp</param-value> </init-param> 
+1
source

Have you tried to use input stream?

Like:

 @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public Response post( @Context HttpServletRequest request, @Context HttpHeaders headers, @FormDataParam("file") InputStream fileStream, 

Works great for me.

0
source

Source: https://habr.com/ru/post/915786/


All Articles