How to add new header to jersey client to upload multipart file

cxyz

Please find below jersey client code to upload multipart file:

String url = "http://localhost:7070"
Client client = Client.create();
WebResource webresource = client.resource(url);
File file = new File("C://Data//image1.jpg");
File thumbnail = new File("C://Data/image2.jpg");


InputStream isthumbnail = new FileInputStream(thumbnail);
InputStream isfile = new FileInputStream(file);

FormDataMultiPart multiPart = new FormDataMultiPart();
FormDataBodyPart bodyPart1 = new FormDataBodyPart(FormDataContentDisposition.name("Thumbnail").fileName("thumbnail").build(), isthumbnail, MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataBodyPart bodyPart2 = new FormDataBodyPart(FormDataContentDisposition.name("File").fileName("file").build(), isfile, MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(bodyPart);
multiPart.bodyPart(bodyPart1);

//New Headers
String fileContentLength = "form-data; contentLength=\""+Long.toString(file.length())+ "\"";
String thumbnailContentLength = "form-data; contentLength=\""+Long.toString(file.length())+ "\"";

final ClientResponse clientResp = webresource.type(MediaType.MULTIPART_FORM_DATA_TYPE).accept(MediaType.APPLICATION_XML).post(ClientResponse.class, multiPart);
System.out.println("File Upload Success with Response"+clientResp.getStatus());

I need to add the String fileContentLength and thumbnailContentLength as header Content-Length. How do i add the headers as part of multipart and post the request?Any help would be appreciated

user1907906

Use a FormDataContentDisposition as an argument to FormDataBodyPart(FormDataContentDisposition formDataContentDisposition, Object entity, MediaType mediaType).

final FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
final String value = "Hello World";
final FormDataContentDisposition dispo = FormDataContentDisposition
        .name("file")
        .fileName("test.txt")
        .size(value.getBytes().length)
        .build();
final FormDataBodyPart bodyPart = new FormDataBodyPart(dispo, value);
formDataMultiPart.bodyPart(bodyPart);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Jersey Image Upload Client

分類Dev

Apache Camel - Multipart File upload

分類Dev

Webflux, How to intercept a request and add a new header

分類Dev

How to add new line in a file

分類Dev

Spring Boot Multipart File Upload Size Inconsistency

分類Dev

Simple Odata Client - How to add oAuth Token in each request header?

分類Dev

How to change the CookieSpec in Jersey Client?

分類Dev

Spring WebFlux File Upload: Unsupported Media Type 415 with Multipart upload

分類Dev

How to check if the multipart upload was successful with alamofire image

分類Dev

How to add chef-client recipe on new born client node (after client register)

分類Dev

How to add blank rows or custom header in excel export file in datatables?

分類Dev

Upload File Without Multipart/Form-Data Using RestSharp

分類Dev

Spring rest MultiPart file upload with Java configuration without Spring boot

分類Dev

How to limit size of multipart file in a java controller

分類Dev

AWS Multipart Upload SignatureDoesNotMatch

分類Dev

Jersey: How to Add Jackson to Servlet Holder

分類Dev

How to upload a file to an API

分類Dev

How can I upload a new revision to an specific file in Google Drive using the REST API?

分類Dev

How to handle client abort during request upload?

分類Dev

How to handle client abort during request upload?

分類Dev

add a new line to a delimited file

分類Dev

S3 multipart upload - complete multipart upload asyncronously

分類Dev

upload array of images with alamofire multipart

分類Dev

Show upload image progress Multipart

分類Dev

How to upload file using Koa?

分類Dev

How to upload file to HDFS in Ubuntu

分類Dev

how to upload a pdf file to ftp server using file upload controller?

分類Dev

How to Add/Activate "Oracle.ManagedDataAccess.Client" in the EF 6 provider config file?

分類Dev

how to use embedded expressions for multipart file input in karate

Related 関連記事

  1. 1

    Jersey Image Upload Client

  2. 2

    Apache Camel - Multipart File upload

  3. 3

    Webflux, How to intercept a request and add a new header

  4. 4

    How to add new line in a file

  5. 5

    Spring Boot Multipart File Upload Size Inconsistency

  6. 6

    Simple Odata Client - How to add oAuth Token in each request header?

  7. 7

    How to change the CookieSpec in Jersey Client?

  8. 8

    Spring WebFlux File Upload: Unsupported Media Type 415 with Multipart upload

  9. 9

    How to check if the multipart upload was successful with alamofire image

  10. 10

    How to add chef-client recipe on new born client node (after client register)

  11. 11

    How to add blank rows or custom header in excel export file in datatables?

  12. 12

    Upload File Without Multipart/Form-Data Using RestSharp

  13. 13

    Spring rest MultiPart file upload with Java configuration without Spring boot

  14. 14

    How to limit size of multipart file in a java controller

  15. 15

    AWS Multipart Upload SignatureDoesNotMatch

  16. 16

    Jersey: How to Add Jackson to Servlet Holder

  17. 17

    How to upload a file to an API

  18. 18

    How can I upload a new revision to an specific file in Google Drive using the REST API?

  19. 19

    How to handle client abort during request upload?

  20. 20

    How to handle client abort during request upload?

  21. 21

    add a new line to a delimited file

  22. 22

    S3 multipart upload - complete multipart upload asyncronously

  23. 23

    upload array of images with alamofire multipart

  24. 24

    Show upload image progress Multipart

  25. 25

    How to upload file using Koa?

  26. 26

    How to upload file to HDFS in Ubuntu

  27. 27

    how to upload a pdf file to ftp server using file upload controller?

  28. 28

    How to Add/Activate "Oracle.ManagedDataAccess.Client" in the EF 6 provider config file?

  29. 29

    how to use embedded expressions for multipart file input in karate

ホットタグ

アーカイブ