Status Code 415: No MessageBodyReader Found for mu

2019-07-05 04:12发布

I'm running into some trouble POSTing multipart/form-data to a RESTful web service that I've made. I am trying to upload media files (images, video, audio) via a RESTful web service. I googled around to find the best way to do this and found that POSTing multipart/form-data was the best solution.

The problem is when I POST some multipart/form-data I get this error message in my Tomcat server:

SEVERE MessageBodyReader not found for media type=multipart/form-data; boundary=----WebKitFormBoundaryTg7uVLcYJ3lsBpQE, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.

I did try looking on stackoverflow to find an answer and the problem seemed to be that a mimepull.jar was missing for many people. I checked to make sure that a mimepull.jar was in my classpath and indeed it is, so this is not the issue. At this point I'm stuck.

Here are my dependencies in my pom.xml:

<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.12</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.12</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.12</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.12</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.2</version>
    </dependency>
</dependencies>

Here is the relevant backend code that handles multipart/form-data POSTs:

@POST
@Path("media")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(FormDataMultiPart form ) {

    FormDataBodyPart filePart = form.getField("file");
    ContentDisposition headerofFilePart = filePart.getContentDisposition();
    InputStream uploadedInputStream = filePart.getValueAs(InputStream.class);

    String uploadedFileLocation = "C:\\surveymedia\\media" + headerofFilePart.getFileName();

    try {
        saveFile(uploadedInputStream, uploadedFileLocation);
    } catch (Exception e) {
        return Response.status(400).entity(e.getCause()).build();
    }

    String output = "File uploaded to: " + uploadedFileLocation;
    return Response.status(200).entity(output).build();
}

Lastly here is the test page I made to test sending files to the backend:

<html>
   <head>
      <title></title>
   </head>
   <body>
      <h1>File Upload with Jersey</h1>

      <form action="/rest/surveys/media" method="post" enctype="multipart/form-data">

         <p>
            Select a file : <input type="file" name="file" size="45" />
         </p>

         <input type="submit" value="Upload It" />
      </form>
   </body>
</html>

Let me know if you need more information. Thanks in advance for the help!

1条回答
淡お忘
2楼-- · 2019-07-05 04:32

I ran into this similar issue when implementing a file uploader via Jersey, so I ended up taking a slightly different approach where the method's parameters are the InputStream and a FormDataContentDisposition object.

Here's an example; perhaps this will work for you:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.APPLICATION_JSON})
public Response uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) throws Exception {

    String filename = fileDetail.getFileName();
    String uploadedFileLocation = "C:\\surveymedia\\media" + filename;
    try {
        saveFile(uploadedInputStream, uploadedFileLocation);
    }
    catch(Exception e){
        return Response.status(400).entity(e.getCause()).build();
    }

    String output = "File uploaded to: " + uploadedFileLocation;
    return Response.status(200).entity(output).build();
}
查看更多
登录 后发表回答