Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未找到com.sun.jersey.core.header.FormDataContentDisposition类的消息正文读取器,ContentType:多部分/表单数据_Java_Rest_Maven_Jersey - Fatal编程技术网

Java 未找到com.sun.jersey.core.header.FormDataContentDisposition类的消息正文读取器,ContentType:多部分/表单数据

Java 未找到com.sun.jersey.core.header.FormDataContentDisposition类的消息正文读取器,ContentType:多部分/表单数据,java,rest,maven,jersey,Java,Rest,Maven,Jersey,我有一个jersey服务器和客户端。我已经尝试了几乎所有的方法,但我无法将文件从客户端发送到服务器。我从服务器端使用此方法: @Path("/" + PathConstants.UPLOAD_RESOURCE) public class UploadResource { @POST @Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_OCTET_STREAM, MediaType.MULTI

我有一个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>
    <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>

有谁能告诉我哪里错了,因为我花了几天的时间试图解决这个问题,但我仍然无法使它正常工作。

基本上,您的后端代码是正确的。在“消费”中对不必要的东西表示粗鲁。这里只需要多部分表单数据

@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是否同时在服务器和客户机上注册(在本例中我是为您注册的)。
另外,它看起来像是在遗留Jersey上,考虑更新到

< p>我也忘了提到我使用HTTPASBICCu认证的客户端代码,这样服务器就可以验证发送请求的用户,如果我将客户端修改为您建议如何验证用户的版本。查看客户端代码:

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());
}

谢谢你的回答!真的很感激!为了确保我理解你的意思是正确的,您建议使用org.glassfish而不是com.sun jersey依赖项?我的建议:1)删除@Consumes中不必要的媒体类型2)检查为服务器端和客户端注册的多部分功能3)移动到org.glassfish jars而不是com.sun提到的异常,该异常通常在功能未注册或未注册时引发适合当前媒体类型的处理程序。在第二种情况下,您需要放置正确的罐子。不幸的是,Jersey网站现在关闭了,所以我无法为您的版本指向正确的jar。这就是我建议升级的原因。我正在使用HTTP基本身份验证,如果我使用您提到的这个客户端,我将如何发送身份验证头。请看一下用于发送文件的客户端代码:以及如何注册MultiPartFeature?客户端的多部分注册=ClientBuilder.newBuilder().register(MultiPartFeature.class).build();//服务器注册取决于您如何创建应用程序。例如,如果您正在扩展ResourceConfig,您可以这样做:public类YourWebApp扩展ResourceConfig{public YourWebApp(){register(MultiPartFeature.class);}}}}基本身份验证应该像HttpAuthenticationFeature authFeature=HttpAuthenticationFeature.Basic(“用户名”“密码”);客户端注册(authFeature);
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));
        }
}
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());
}