泽西休息返回400文件上传/多部分

时间:2017-01-24 21:05:29

标签: jersey jersey-2.0 jersey-client

测试用例使用此测试用例上传文件和表单数据

    final Client client =     ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
    WebTarget t = client.target("http://localhost:8080/context/dnt/post").path("upload2");

    PostTestCases postTestCases=new PostTestCases();
    String file1=postTestCases.getFileName("images/ny.jpeg");
    String file2=postTestCases.getFileName("images/sf.jpeg");

    FileDataBodyPart filePart = new FileDataBodyPart("files", new File(file1));
    filePart.setContentDisposition(FormDataContentDisposition.name("files").fileName("ny.jpeg").build());

    FileDataBodyPart filePart1 = new FileDataBodyPart("files", new File(file2));
    filePart1.setContentDisposition(FormDataContentDisposition.name("files").fileName("sf.jpeg").build());

    PostDTO post = populatePostDTO();

    ObjectMapper mapper = new ObjectMapper();
    String value = mapper.writeValueAsString(post);

    MultiPart multipartEntity = new FormDataMultiPart().field("postDTO", value, MediaType.APPLICATION_JSON_TYPE)
            .bodyPart(filePart).bodyPart(filePart1);

    Response response = t.request().post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA));
    System.out.println(response.getStatus());
    System.out.println(response.readEntity(String.class));

    response.close();

响应:来自测试用例的响应

INFO: 2 * Server has received a request on thread http-nio-8080-exec-4
2 > POST http://localhost:8080/context/dnt/post/upload2
2 > accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
2 > connection: keep-alive
2 > content-length: 19855
2 > content-type: multipart/form-      data;boundary=Boundary_1_1326393666_1485290224667
2 > host: localhost:8080
2 > mime-version: 1.0
2 > user-agent: Jersey/2.25 (HttpUrlConnection 1.8.0_77)

Jan 24, 2017 3:37:04 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 2 * Server responded with a response on thread http-nio-8080-exec-4
2 < 400

方法:这是我用来接受图片和数据的方法

   @POST
   @Path("/upload2")
   @Consumes({MediaType.MULTIPART_FORM_DATA})
   public Response uploadFileWithData(
        @FormDataParam("files") List<FormDataBodyPart> bodyParts,
        @FormDataParam("files") FormDataContentDisposition fileDispositions,
        @FormDataParam("postDTO") PostDTO postDTO) throws Exception{


    Post post = mapper.map(postDTO, Post.class);
    postService.createPost(bodyParts,post);

    return Response.ok("Cool Tools!").build();

从Rest Client添加图片 Request

我尝试添加jersey-multipart-config.properties,但仍然给我400个错误请求。

0 个答案:

没有答案