Post 使用 java 调用文件上传未按预期工作

Post call for file upload not working as expected using java

Post 使用 java.I 调用文件上传未按预期工作需要使用 rest 调用上传文件..文件格式正确并且在 postman 和 [=22= 中完美运行] 方面,但在 java 中,它给出了“不正确的文件格式”,因为文件似乎没有上传。我是否遗漏了任何 header 或任何东西。

File file = new File("/Users/surya/Downloads/2021-06-16.xlsx");
        
          Response response = RestAssured
                    .given()
                    .multiPart("file", file, "multipart/form-data")                
                    .post("http://myuploadsite.com/upload/feedfile");         
            System.out.println(response.asString());

我的 postman curl 请求

curl --location --request POST 'http://myuploadsite.com/upload/feedfile' \
--header 'Content-Type: multipart/form-data; boundary=jena' \
--header 'Host;' \
--form 'file=@"/Users/surya/Downloads/2021-06-16.xlsx"'

您使用的方法是:

RequestSpecification multiPart(String controlName, File file, String mimeType);

您需要为要上传的文件定义 mimeType,在本例中为 application/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.ms-excel。我不太确定是哪一个。

或者留空使用重载方法:

RequestSpecification multiPart(String controlName, File file);

代码为:

File file = new File("/Users/surya/Downloads/2021-06-16.xlsx");
        
Response response = RestAssured
      .given()
      .multiPart("file", file, "application/vnd.ms-excel")                
      .post("http://myuploadsite.com/upload/feedfile");         
System.out.println(response.asString());
当您使用 multipart

时,

Content-Type: multipart/form-data 将由 Rest-Assured 自动定义

https://github.com/rest-assured/rest-assured/wiki/Usage#multi-part-form-data