415使用Jersey Servlet上传文件时出错

时间:2018-04-17 19:34:20

标签: java jersey jax-rs

使用test.html上传文件时,出现HTTP 415错误,不支持的媒体类型。在球衣中使用multipart似乎无法正常工作。在我的服务器的控制台中,我收到以下错误:

No message body reader has been found for class org.glassfish.jersey.media.multipart.FormDataContentDisposition,
    ContentType: multipart/form-data;boundary=----WebKitFormBoundaryFwksqvOnuCUBmh87

在对Web和其他StackOverflow文章进行一些研究后,我找不到合适的解决方案。我见过的一个反复出现的主题是关于注册Jersey多部分功能,但我不清楚我的文件结构中在哪里或如何做到这一点。我认为我接近一个有效的解决方案,但还没有。这是我目前的代码:

项目结构

Project Structure的pom.xml

<dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>3.1.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_3.0_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.cloudant</groupId>
        <artifactId>cloudant-client</artifactId>
        <version>2.7.0</version>
    </dependency>

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>

    <dependency>
        <groupId>com.ibm.watson.developer_cloud</groupId>
        <artifactId>java-sdk</artifactId>
        <version>4.2.1</version>
    </dependency>

    <dependency>
        <groupId>com.box</groupId>
        <artifactId>box-java-sdk</artifactId>
        <version>2.8.2</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.27</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.27</version>
    </dependency>

    <dependency>
        <groupId>org.jvnet.mimepull</groupId>
        <artifactId>mimepull</artifactId>
        <version>1.9.7</version>
    </dependency>

</dependencies>

UploadFiles.java

package wasdev.sample.rest;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;


@ApplicationPath("api")
@Path("/upload")
public class UploadFiles{
    @POST
    @Path("/")
    @Consumes({MediaType.MULTIPART_FORM_DATA})
    public Response uploadPdfFile(
            @FormDataParam("file") InputStream fileInputStream,
            @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
    {
        String filename = fileMetaData.getFileName();
        String UPLOAD_PATH = "\\home\\andy" + filename;
        try
       {
           int read = 0;
           byte[] bytes = new byte[1024];

           OutputStream out = new FileOutputStream(new File(UPLOAD_PATH));
            while ((read = fileInputStream.read(bytes)) != -1)
            {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e)
        {
            throw new WebApplicationException("Error while uploading file. Please try again !!");
        }
        return Response.ok("Data uploaded successfully !!").build();
    }
}

的test.html

<html>
<body>
<h1>File Upload Example - howtodoinjava.com</h1>

<form action="http://localhost:9080/GetStartedJava/api/upload" method="post" enctype="multipart/form-data">

    <p>Select a file : <input type="file" name="file" size="45" accept=".jpg" /></p>
    <input type="submit" value="Upload PDF" />

</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

首先,从UploadFiles类中删除@ApplicationPath注释。仅使用@Path来定义资源类。

创建一个扩展ApplicationResourceConfig的类,使用@ApplicationPath对其进行注释,然后注册MultipartFeature类:

@ApplicationPath("/api")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(MultipartFeature.class);
        return classes;
    }
}
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        register(MultipartFeature.class);
    }
}

您可能还需要注册资源类。有关Application的详细信息,请参阅此answer