状态码415:没有为多部分/表单数据找到MessageBodyReader,FormDataMultiPart

时间:2014-10-27 14:58:02

标签: java rest maven tomcat jersey

我在将multipart / form-data发布到我已经制作的RESTful Web服务时遇到了一些麻烦。我正在尝试通过RESTful Web服务上传媒体文件(图像,视频,音频)。我搜索了最好的方法,发现POST多部分/表格数据是最好的解决方案。

问题是当我发布一些multipart / form-data时,我在Tomcat服务器中收到此错误消息:

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.

我确实尝试在stackoverflow上查找答案,问题似乎是很多人都缺少mimepull.jar。我检查确保mimepull.jar在我的类路径中,确实是,所以这不是问题。此时我被困住了。

以下是我在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>

以下是处理multipart / form-data POST的相关后端代码:

@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();
}

最后,这是我测试发送文件到后端的测试页面:

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

如果您需要更多信息,请与我们联系。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我在通过Jersey实现文件上传器时遇到了类似的问题,所以我最终采用了一种略有不同的方法,其中方法的参数是InputStream和FormDataContentDisposition对象。

这是一个例子;也许这对你有用:

@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();
}