上载JAX-rs时,文件上传给出415不支持的媒体类型

问题描述

当我尝试在JAX-RS Jersey中上载文件时,出现错误415不支持媒体类型。 我已经附上了我使用的代码配置文件,请有人帮我。我还尝试使用@MultipartConfig 为该类添加注释。我没有在邮递员中传递任何标题

删除FormDataContentdisposition将删除错误,并且文件上传有效,但是我想知道为什么FormDataContentdisposition在我的代码中完全不起作用

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@OAuthSecurity(enabled = false)
@Path("/upload")
public Response postForm(
    @FormDataParam("file") InputStream file,@FormDataParam("file") FormDataContentdisposition filedisposition) {
    return Response.ok().build();    
}

我的pom.xml

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.ibm.mfp/adapter-maven-api -->
    <dependency>
        <groupId>com.ibm.mfp</groupId>
        <artifactId>adapter-maven-api</artifactId>
        <version>8.0.2018071507</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.4.RELEASE</version>
        <type>jar</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.7</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-java-token-validator -->
    <dependency>
        <groupId>com.ibm.mfp</groupId>
        <artifactId>mfp-java-token-validator</artifactId>
        <version>8.0.2017020112</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-security-checks-base -->
    <dependency>
        <groupId>com.ibm.mfp</groupId>
        <artifactId>mfp-security-checks-base</artifactId>
        <version>8.0.2018030404</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>



    <!-- optional,good for handle I/O task -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.0.1</version>
    </dependency>



    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.4</version>
    </dependency>

**<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.5.1</version>
    </dependency>**
</dependencies>

我有一个扩展应用程序的Main类

public class MainApplication extends Application {
    void init() throws Exception {
        //class loading intitalization
        log.info("Initialized");
    }
    
    void destroy() throws Exception {
        log.info("destroyed!");
    }
    
    String getPackagetoScan() {
        // The package of this class will be scanned (recursively) to find JAX-RS resources.
        return getClass().getPackage().getName();
    }
}

没有添加其他配置类,也没有web.xml文件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)