[Bugfix][Java] Fixed jersey clients for multiple file upload (#19476)

* Fixed jersey3 client for multiple file upload

* Updated sample

* Fixed typo

* Fix

* Fix for jersey2
This commit is contained in:
Matteo Molinari
2024-09-02 11:25:54 +02:00
committed by GitHub
parent c733bb69a2
commit fb1c2f3483
12 changed files with 396 additions and 204 deletions

View File

@@ -908,24 +908,10 @@ public class ApiClient extends JavaTimeFormatter {
if (contentType.startsWith("multipart/form-data")) {
MultiPart multiPart = new MultiPart();
for (Entry<String, Object> param: formParams.entrySet()) {
if (param.getValue() instanceof File) {
File file = (File) param.getValue();
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
.fileName(file.getName()).size(file.length()).build();
// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
if (param.getValue() instanceof Iterable) {
((Iterable)param.getValue()).forEach(v -> addParamToMultipart(v, param.getKey(), multiPart));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
addParamToMultipart(param.getValue(), param.getKey(), multiPart);
}
}
entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
@@ -954,6 +940,36 @@ public class ApiClient extends JavaTimeFormatter {
return entity;
}
/**
* Adds the object with the provided key to the MultiPart.
* Based on the object type sets Content-Disposition and Content-Type.
*
* @param obj Object
* @param key Key of the object
* @param multiPart MultiPart to add the form param to
*/
private void addParamToMultipart(Object value, String key, MultiPart multiPart) {
if (value instanceof File) {
File file = (File) value;
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key)
.fileName(file.getName()).size(file.length()).build();
// Attempt to probe the content type for the file so that the form part is more correctly
// and precisely identified, but fall back to application/octet-stream if that fails.
MediaType type;
try {
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
} catch (IOException | IllegalArgumentException e) {
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
} else {
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(key).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(value)));
}
}
/**
* Serialize the given Java object into string according the given
* Content-Type (only JSON, HTTP form is supported for now).