在 Java 中使用 Unirest 的分段文件上传请求

Bha*_*ani 4 java post file-upload multipart unirest

我可以使用 REST 客户端 (Insomnia) 发布此请求。但是,当我无法编写正确的代码在 Java 中执行相同操作时。下面是我的失眠请求的样子。

在此处输入图片说明

下面是客户端生成的代码的样子。

HttpResponse<String> response = Unirest.post("http://172.16.6.15:5053/image-service/services/image-panel-service/panel/images?=")
  .header("com.yatra.tenant.header.tenantid", "1051")
  .header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
  .body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"imageFile\"\r\n\r\n")
  .asString();
Run Code Online (Sandbox Code Playgroud)

下面是我用 Java 编写的代码,但它不起作用。

try {
            HttpResponse<String> response = Unirest.post("http://172.16.6.15:5053/image-service/services/image-panel-service/panel/images")
            .header("com.yatra.tenant.header.tenantid", "1051")
            .header("content-type", "multipart/form-data")
            .field("imageFile", new File("Desert.jpg"))
            .field("imageData", new File("ImageUploadRequest.json")).asString();

            System.out.println(response.getBody());

        } catch (UnirestException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

小智 5

摘自他们的文档 http://kong.github.io/unirest-java/#file-uploads

 Unirest.post("http://httpbin.org")
   .field("imageFile", new File("JellyFirst.jpg"))
   .asEmpty();
Run Code Online (Sandbox Code Playgroud)