Http 415 on file Upload using jersey

后端 未结 5 463
花落未央
花落未央 2020-12-10 07:36

My code for RESTful file upload :

@Path(\"/upload\") 
@POST 
@Consumes(\"multipart/form-data\") 
public String post(
    @FormDataParam(\"part\") String s, 
         


        
相关标签:
5条回答
  • 2020-12-10 08:08

    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)

    0 讨论(0)
  • 2020-12-10 08:10

    Have you tried with an Input stream ?

    Like :

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

    Works fine for me.

    0 讨论(0)
  • 2020-12-10 08:27

    This can happen due to a couple of reasons. I managed to narrow down some of them.

    1. Your Content-Type header does not match with the one provided by the @Consumes header. Verify this with a proxy.

    2. You managed to stumble upon a bug that was fixed in Jersey 1.4 related to the FormDataParam annotation.

    3. You included jersey-bundle and jersey-server et all in the same binary and they are competing against each other.

    4. You are using @FormParam instead of @FormDataParam.

    5. 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.

    0 讨论(0)
  • 2020-12-10 08:28

    Please make sure you have mimepull.jar on the classpath

    0 讨论(0)
  • 2020-12-10 08:32

    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:

    /**
     * 
     */
    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 Jersey servlet in web.xml:

         <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.verico.multipart.app.MultiPartApp</param-value>
        </init-param>
    
    0 讨论(0)
提交回复
热议问题