多个文件上传,放心使用

时间:2017-01-28 11:42:07

标签: java postman rest-assured

我有一个post api调用,允许我在form-data参数中上传多个图像,名为" file" 在邮递员中,我可以在表单数据中发送多个文件,其中包含密钥" file"和值作为多个文件

我想用放心的Java客户端模拟相同的调用。我可以上传单个文件并确保无法上传多个文件。

以下是单个文件上传的保证代码:

File sheetFile = new File(sheetPath.get(0));
response = RestAssured.
          given().
              headers(headers).
              formParameter("studentDetail",
                      "{"+
                      "\"userId\" : "+PropFileHandler.readProperty("psUserId")+
                      ",\"institutionId\" : "+PropFileHandler.readProperty("institution_id")+
                      ",\"externalUserId\" : "+PropFileHandler.readProperty("user_id")+
                      ",\"tokenId\" : \"12000\""+
                      ",\"imagePathStatus\" : \"\""+
                      "}").
              formParameter("clientType", getData("ps_bulk_upload_image.clientType")).
              multiPart("file", sheetFile). #here i want to upload multile files
          when().
              post(relativePath).
          then().
              statusCode(200).
              body(matchesJsonSchema(new File(test.cngActions.getJsonSchemaDirectoryPath()+
                      getData("ps_bulk_upload_image.schemaPath")))).
              extract().response();

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

只需多次致电multiPart

见这里:https://blog.jayway.com/2011/09/15/multipart-form-data-file-uploading-made-simple-with-rest-assured/

相关代码:

given().
  multiPart("file1", new File("/home/johan/some_large_file.bin")).
  multiPart("file2", new File("/home/johan/some_other_large_file.bin")).
  multiPart("file3", "file_name.bin", inputStream).
  formParam("name", "value").
expect().
  body("fileUploadResult", is("OK")).
when().
  post("/advancedFileUpload");

答案 1 :(得分:0)

您只需要在需要时调用多部分方法使用相同的 controlName

RestAssuredMockMvc
    .given()
        .log().all()
        .headers(httpHeaders)
        .contentType(MediaType.MULTIPART_FORM_DATA.toString())
        .multiPart("content", file1)
        .multiPart("content", file2)

您发送以下请求

Multiparts:     ------------
            Content-Disposition: form-data; name = content; filename = test1.txt
            Content-Type: application/octet-stream

            C:\Users\mgagomez\AppData\Local\Temp\junit7193983138466861544\test1.txt
            ------------
            Content-Disposition: form-data; name = content; filename = test2.txt
            Content-Type: application/octet-stream

            C:\Users\mgagomez\AppData\Local\Temp\junit7193983138466861544\test2.txt