没有为类com.sun.jersey.core.header.FormDataContentDisposition找到消息正文阅读器,ContentType:multipart / form-data

时间:2017-05-17 15:19:37

标签: java rest maven jersey

我有一个球衣服务器和客户端。我几乎尝试了所有东西,但我无法从客户端向服务器发送文件。我在服务器端使用这种方法:

@Path("/" + PathConstants.UPLOAD_RESOURCE)
public class UploadResource {

    @POST
    @Consumes({ MediaType.MULTIPART_FORM_DATA, 
    MediaType.APPLICATION_OCTET_STREAM, MediaType.MULTIPART_FORM_DATA })
    public Response uploadFile(@FormDataParam("file") InputStream 
         uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {
         String uploadedFileLocation = "C:\\Users\\Desktop\\" + 
         fileDetail.getFileName();
         System.out.println(uploadedFileLocation);
         File objFile = new File(uploadedFileLocation);
         if (objFile.exists()) {
            objFile.delete();

        }

        saveToFile(uploadedInputStream, uploadedFileLocation);
        String output = "File uploaded via Jersey based RESTFul Webservice to: " 
        + uploadedFileLocation;
        return Response.status(200).entity(output).build();
    }
private void saveToFile(InputStream uploadedInputStream, String uploadedFileLocation) {

        try {
            OutputStream out = null;
            int read = 0;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

我的服务器pom.xml依赖项:

      <repositories>
      <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
  </repositories>

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.19.3</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.19.3</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.19.3</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.19</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.19.3</version>
    </dependency>

我的服务器web.xml:

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  id="WebApp_ID" version="3.1">

<display-name>test</display-name>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>server-web</servlet-name>
    <servlet-class>
                 com.sun.jersey.spi.container.servlet.ServletContainer
            </servlet-class>
    <init-param>
         <param-name>com.sun.jersey.config.property.packages</param-name>
         <param-value>com.server</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>com.sun.jersey.multipart.MultiPart</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>server-web</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

    Client side code:

    @Override
    public void execute(CommandArgument arguments) {
    File file = new File("C:\\addfile.txt");
    final ClientConfig config = new DefaultClientConfig();
    final Client client = Client.create(config);
    client.addFilter(new LoggingFilter());
    client.addFilter(
            new HTTPBasicAuthFilter(CommandLineRestClient.currentUsername, 
    CommandLineRestClient.currentPassword));
    WebResource resource = client.resource(PathConstants.UPLOAD_FILE);
    FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
    formDataMultiPart.bodyPart(new FileDataBodyPart("file", file, 
    MediaType.APPLICATION_OCTET_STREAM_TYPE));
    ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class,
            formDataMultiPart);
    System.out.println("Response: " + response.getStatus());
}

和我的客户端依赖项:

    <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.19</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.19</version>
    </dependency>

堆栈跟踪:

     WARNING: javax.ws.rs.WebApplicationException: HTTP 415 Unsupported 
Media Type
at 
org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody
(JAXRSUtils.java:1315)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter
(JAXRSUtils.java:826)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters
(JAXRSUtils.java:789)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest
(JAXRSInInterceptor.java:212)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage
(JAXRSInInterceptor.java:77)

任何人都可以告诉我我错在哪里,因为我试图解决这个问题几天,但我仍然无法使其发挥作用。

2 个答案:

答案 0 :(得分:0)

基本上,您使用后端代码以正确的方式运行。在&#34;消费&#34;中得到粗鲁的不必要的东西。这里只需要多部分表格数据。

@Consumes( MediaType.MULTIPART_FORM_DATA )

我怀疑问题是客户端或依赖关系。 所以客户端应该是这样的:

public void testUploading() {
        File file = new File("C:\\your_test_file_to_upload.jpg");
        FileDataBodyPart fileDataPart = new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE);
        try (FormDataMultiPart formDataMultiPart = new FormDataMultiPart()) {
                MultiPart multipartEntity = formDataMultiPart.bodyPart(fileDataPart);

                Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();

                Response response = client.target("/" + PathConstants.UPLOAD_RESOURCE)
                        .request()
                        .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA));
        }
}

检查服务器上注册的MultiPartFeature&amp;客户端(我在示例中为您完成了)两者。 此外,看起来您已经使用旧版Jersey,请考虑更新为jersey-media-multipart

答案 1 :(得分:0)

我也忘了提及我使用HttpBasicAuthentication的客户端代码,以便服务器可以验证发送请求的用户如果我将客户端修改为您建议的版本如何验证用户。看看客户端代码:

public class UploadCommand implements Command {

@Override
public void execute(CommandArgument arguments) {
    File file = new File("C:\\addfile.txt");
    final ClientConfig config = new DefaultClientConfig();
    final Client client = Client.create(config);
    client.addFilter(new LoggingFilter());
    client.addFilter(
            new HTTPBasicAuthFilter(CommandLineRestClient.currentUsername, CommandLineRestClient.currentPassword));
    WebResource resource = client.resource(PathConstants.UPLOAD_FILE);
    FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
    formDataMultiPart.bodyPart(new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
    ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class,
            formDataMultiPart);
    System.out.println("Response: " + response.getStatus());
}