【问题标题】:Unsupported Media Type error in Jersey APIJersey API 中不支持的媒体类型错误
【发布时间】:2017-04-06 10:58:49
【问题描述】:

我在 stackoverflow 上看到了多个答案,但无法确定我的 pom.xml 有什么问题。 我必须在服务器上上传文件,所以我使用的是this link的FormDataMultiPart的修改代码

我的代码为:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.media.multipart.ContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;

@Path("/files")
public class JerseyFileUpload {

private static final String SERVER_UPLOAD_LOCATION_FOLDER = "E:/Upload_Files/";

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(FormDataMultiPart form) {

     FormDataBodyPart filePart = form.getField("file");

     ContentDisposition headerOfFilePart =  filePart.getContentDisposition();

     InputStream fileInputStream = filePart.getValueAs(InputStream.class);

     String filePath = SERVER_UPLOAD_LOCATION_FOLDER + headerOfFilePart.getFileName();

    // save the file to the server
    saveFile(fileInputStream, filePath);

    String output = "File saved to server location using FormDataMultiPart : " + filePath;

    return Response.status(200).entity(output).build();

}

// save uploaded file to a defined location on the server
private void saveFile(InputStream uploadedInputStream, String serverLocation) {

    try {
        OutputStream outpuStream = new FileOutputStream(new File(
                serverLocation));
        int read = 0;
        byte[] bytes = new byte[1024];

        outpuStream = new FileOutputStream(new File(serverLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            outpuStream.write(bytes, 0, read);
        }

        outpuStream.flush();
        outpuStream.close();

        uploadedInputStream.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

 }

我的 pom.xlm 是这样的

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>Photographer</groupId>
<artifactId>Assignment</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Assignment</name>

<build>
    <finalName>Assignment</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <!-- uncomment this to get JSON support-->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

   <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.18</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.25</version>
    </dependency>               

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
    </dependency>

</dependencies>
<properties>
    <jersey.version>2.16</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

我使用的是 Jersey 版本 2.16,我仍然收到“不支持的媒体类型”,即使我使用的是 jersey-media-multipart 版本 2.25。是不是版本有问题?或者这个 pom.xml 有什么问题??

谢谢,如果有任何帮助,将不胜感激。

【问题讨论】:

  • 你能在你的uploadFile 方法参数中使用@FormDataParam("file") InputStream fileInputStream,并检查一下吗?
  • 怎么样??不明白.. 而不是 FormDataBodyPart 我应该使用 FormDataParam?
  • 它不起作用,我收到错误 500,“org.glassfish.jersey.server.model.ModelValidationException:应用程序资源模型的验证在应用程序初始化期间失败。”

标签: java rest maven jersey jersey-2.0


【解决方案1】:

您仍然需要注册jersey-media-multipart 附带的MultiPartFeature。该功能将注册处理多部分请求所需的提供程序。您当前看到的错误是因为没有注册可以处理FormDataMultiPart 的提供程序。请参阅this post 了解几种不同的注册方式

【讨论】:

    猜你喜欢
    • 2015-08-21
    • 2015-04-23
    • 2016-06-06
    • 2020-12-23
    • 1970-01-01
    • 2020-05-12
    • 2014-11-11
    相关资源
    最近更新 更多