File upload IBM Mobilefirst Java HTTP适配器中的多部分文件上载

File upload IBM Mobilefirst Java HTTP适配器中的多部分文件上载,file-upload,jax-rs,ibm-mobilefirst,server-side,multipart,File Upload,Jax Rs,Ibm Mobilefirst,Server Side,Multipart,我是IBM Mobile first java adapter的新手,我正在尝试使用Postman向我的java适配器(使用JAX Rs)发送一个映像文件(多部分请求),并在Rest API中接收该映像文件,然后将其发送到第三方服务器 但是,每当我从Postman中点击API时,就会出现415个不受支持的媒体类型错误,我甚至尝试在MFPJAXRSApplication类中配置JAX-rs2.0,但这也不起作用。谁能帮我一下吗 我的主要应用程序类 package com.sample.adapte

我是IBM Mobile first java adapter的新手,我正在尝试使用Postman向我的java适配器(使用JAX Rs)发送一个映像文件(多部分请求),并在Rest API中接收该映像文件,然后将其发送到第三方服务器

但是,每当我从Postman中点击API时,就会出现415个不受支持的媒体类型错误,我甚至尝试在MFPJAXRSApplication类中配置JAX-rs2.0,但这也不起作用。谁能帮我一下吗

我的主要应用程序类

package com.sample.adapter;

import java.util.logging.Logger;
import com.ibm.mfp.adapter.api.MFPJAXRSApplication;

public class JavaAdapterApplication extends MFPJAXRSApplication{

    static Logger logger = Logger.getLogger(JavaAdapterApplication.class.getName());

    @Override
    protected void init() throws Exception {
        logger.info("Adapter initialized!");
    }

    @Override
    protected String getPackageToScan() {
        //The package of this class will be scanned (recursively) to find JAX-RS 2.0 resources.
        return getClass().getPackage().getName();
    }

    @Override
    public Set<Class<?>> getClasses() {
        
        
        log.info("********* added this part by following few suggestin of stack overflow ************");
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        resources.add(MultiPartFeature.class);
        addRestResourceClasses(resources);
        return resources;
    }

    /**
     * Do not modify addRestResourceClasses() method. It is automatically populated
     * with all resources defined in the project. If required, comment out calling
     * this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        log.info("********* added this part by following few suggestin of stack overflow ************");
        resources.add(com.adapter.application.services.TestService.class);
    }
}
Pom.xml

    <properties>
        <!-- Use UTF-8 as the encoding of the adapter -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        
    </properties>

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

    
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3.1</version>
</dependency>
    
   
   
     <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.7</version>
        </dependency>     

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.ibm.mfp</groupId>
                <artifactId>adapter-maven-plugin</artifactId>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

UTF-8
com.ibm.mfp
适配器maven api
8.0.2018071507
假如
org.springframework
弹簧网
3.0.4.1发布
罐子
log4j
log4j
1.2.17
org.apache.commons
公地io
1.3.2
com.fasterxml.jackson.core
杰克逊注释
2.9.7
com.ibm.mfp
mfp java令牌验证程序
8.0.2017020112
com.ibm.mfp
mfp安全检查基地
8.0.2018030404
com.google.code.gson
格森
2.8.5
org.apache.httpcomponents
httpime
4.3.1
org.glassfish.jersey.containers
jersey容器servlet
2.7
org.glassfish.jersey.media
泽西岛媒体多部分
2.7
com.ibm.mfp
适配器maven插件
真的
我想知道为什么服务器无法识别多部分请求,我们是否必须配置适配器以接受多部分请求,如果是,那么我们如何配置它?谁能帮我解决这个问题来救我的命


另外,我的应用程序没有WEB.xml或任何以@ApplicationPath开头的类。您可以使用org.apache.commons.fileupload库。适用于IBM MobileFirst适配器的示例代码。 要处理文件,请遵循apache文档

@POST
@Path("attachment")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadAttachment() {
    try {
        FileUploadUtil fileUploadUtil = new FileUploadUtil();
        List<FileItem> items = new ServletFileUpload(fileUploadUtil.createDiskFileItemFactory())
                .parseRequest(request);
        Optional<FileItem> file = fileUploadUtil.findFileItem(items, "file");
        //Your custom logic ....
    } catch (FileUploadException | IOException | MobileException e) {
        // ....
    }
}

在返回此响应之前,它是否抛出服务器端错误/异常?Mobile是否首先使用Jersey实现来支持其JAX-RS?当您省略FormDataContentDisposition时,请求主体显示了什么?它只是“文件”部分的内容,还是完整的多部分实体?我假设是后者。在这种情况下,只需忽略
@FormDataParam
注释。如果不使用第二个参数,第一个参数将被视为整个实体。Jersey的多部分支持将不起作用,除非它与Jersey一起使用。我已经在适配器应用程序MFPJAXRSApplication类中配置了Jersey。。并且在mave文件中导入了jersey dependecy,但同样的问题仍然存在。您可以注入
HttpServletRequest
。您可以查看创建多部分端点是否有效。
@POST
@Path("attachment")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadAttachment() {
    try {
        FileUploadUtil fileUploadUtil = new FileUploadUtil();
        List<FileItem> items = new ServletFileUpload(fileUploadUtil.createDiskFileItemFactory())
                .parseRequest(request);
        Optional<FileItem> file = fileUploadUtil.findFileItem(items, "file");
        //Your custom logic ....
    } catch (FileUploadException | IOException | MobileException e) {
        // ....
    }
}
@Context
private HttpServletRequest request;