Jersey2/3 - Probe content type for multipart upload parts (#14965)

* Probe content type for multipart form uploads since many servers require each part to correctly identify its type.

* Update samples

* Add explanatory comment

* Update samples with comment
This commit is contained in:
John Dimeo
2023-04-14 00:15:13 -04:00
committed by GitHub
parent b247ad75e1
commit 90b78fe97e
9 changed files with 99 additions and 9 deletions

View File

@@ -807,7 +807,17 @@ public class ApiClient extends JavaTimeFormatter {
File file = (File) param.getValue();
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
.fileName(file.getName()).size(file.length()).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
// 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(param.getKey()).build();
multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));