FormDataMultiPart的MessageBodyRea

FormDataMultiPart的MessageBodyRea

本文介绍了状态码415:找不到用于multipart/form-data,FormDataMultiPart的MessageBodyReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将多部分/表单数据发布到我制作的RESTful Web服务时遇到了麻烦.我正在尝试通过RESTful Web服务上传媒体文件(图像,视频,音频).我四处搜寻以找到最佳方法,并发现发布多部分/表单数据是最好的解决方案.

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.

问题是,当我发布一些多部分/表单数据时,我在Tomcat服务器中收到此错误消息:

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.

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

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.

这是我pom.xml中的依赖项:

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>

以下是处理多部分/表单数据POST的相关后端代码:

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!

推荐答案

在通过Jersey实现文件上传器时,我遇到了类似的问题,因此我最终采用了稍微不同的方法,其中方法的参数是InputStream和FormDataContentDisposition对象.

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

这篇关于状态码415:找不到用于multipart/form-data,FormDataMultiPart的MessageBodyReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:56