BAD请求400消耗了multipart / form-data jersey客户端响应java

问题描述 投票:0回答:1

我需要使用服务,该文件是Excel。但是,当我执行消耗时,响应是“返回400错误请求的响应状态”

    String authString = name + ":" + password;
    Client restClient = Client.create();
    String authStringEnc = Base64.getEncoder().encodeToString(authString.getBytes());
    // the file to upload, represented as FileDataBodyPart
    FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", new File(file),
            MediaType.APPLICATION_OCTET_STREAM_TYPE);
    // fileDataBodyPart.setContentDisposition(FormDataContentDisposition.name("file").fileName(file).build());

    FormDataMultiPart multiPart = new FormDataMultiPart();
    multiPart.field("spId", idServicio, MediaType.MULTIPART_FORM_DATA_TYPE).bodyPart(fileDataBodyPart);
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

    WebResource webResource = restClient.resource(url);
    ClientResponse resp = webResource.header("Authorization", "Basic " + authStringEnc)
            .header("Content-Type", "multipart/form-data").post(ClientResponse.class, multiPart);

    String output = resp.getEntity(String.class);
    System.out.print(output);
    return resp;
java http jersey multipartform-data bad-request
1个回答
0
投票

我放置了代理,然后使用Rest客户端“失眠”,它对我来说很有效。这是发送请求的请求:

  1. INSOMNIA

    POST / conf / configuration / distribution-files / service HTTP / 1.1授权:基本aW1wbGluZWE6SU1QTElORUE =用户代理:失眠/7.0.6内容类型:多部分/表单数据;边界= X-INSOMNIA-BOUNDARY接受:/内容长度:5872连接:关闭

    -X-INSOMNIA-BOUNDARY内容处置:表单数据; name =“文件”; filename =“ myspprueba.xls”内容类型:application / vnd.ms-excel

    -X-INSOMNIA-BOUNDARY内容处置:表单数据; name =“ spId”

    5823

  2. [这是我目前失败的要求

    POST /conf/configuration/distribution-files/service HTTP/1.1
    Authorization: Basic aW1wbGluZWE6SU1QTElORUE=
    Accept: */*
    Content-Type: multipart/form-data; boundary=Boundary_1_2104028992_1577117786190
    MIME-Version: 1.0
    User-Agent: Java/1.8.0_211
    Connection: close
    Content-Length: 5994
    

    -Boundary_1_2104028992_1577117786190内容类型:文本/纯文本内容处置:表单数据; filename =“ myspprueba.xls”; modify-date =“星期四,2019年12月19日15:52:46 GMT”;大小= 5632; name =“ file”

© www.soinside.com 2019 - 2024. All rights reserved.