【问题标题】:Multipart File Upload Request Using Unirest in Java在 Java 中使用 Unirest 的多部分文件上传请求
【发布时间】:2019-02-06 08:40:16
【问题描述】:

我可以使用 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();

以下是我用 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();
        }

【问题讨论】:

    标签: java post file-upload multipart unirest


    【解决方案1】:

    他们文档中的片段 http://kong.github.io/unirest-java/#file-uploads

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

    【讨论】:

      【解决方案2】:
      try {
          MultipartBody body = Unirest.post("http://localhost:4849/upload/images")
              .field("name", "bingoo.txt")
              .field("files", temp1)
              .field("files", temp2)
              .field("files", temp3);
          HttpResponse<String> file = body.asString();
          System.out.println(file.getStatus());
      
      } catch (Exception e) {
          e.printStackTrace();
      }
      

      来自https://www.programcreek.com/java-api-examples/?api=com.mashape.unirest.request.body.MultipartBody的sn-p

      【讨论】:

        猜你喜欢
        • 2018-10-25
        • 1970-01-01
        • 2014-11-09
        • 2015-02-22
        • 2017-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多