Skip to content

Instantly share code, notes, and snippets.

@ivanursul
Created June 29, 2016 10:01
Show Gist options
  • Save ivanursul/c484d9223e2a042afa7002f4ac4275a2 to your computer and use it in GitHub Desktop.
Save ivanursul/c484d9223e2a042afa7002f4ac4275a2 to your computer and use it in GitHub Desktop.
private FormDataMultiPart copyMultipart(FormDataMultiPart multiPart) {
FormDataMultiPart copiedMultipart = new FormDataMultiPart();
copiedMultipart.setMediaType(multiPart.getMediaType());
copiedMultipart.setContentDisposition(multiPart.getContentDisposition());
copiedMultipart.getHeaders().putAll(multiPart.getHeaders());
for (BodyPart bodyPart : multiPart.getBodyParts()) {
if (typeEquals(bodyPart.getMediaType(), APPLICATION_OCTET_STREAM_TYPE)) {
FormDataBodyPart formDataBodyPart = (FormDataBodyPart) bodyPart;
/* InputStream in = formDataBodyPart.getEntityAs(InputStream.class);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Try.apply(() -> IOUtils.copy(in, out));
InputStream btis = new ByteArrayInputStream(out.toByteArray());*/
ContentDisposition cd = formDataBodyPart.getContentDisposition();
OutputStream out = new ByteArrayOutputStream();
TeeInputStream in = new TeeInputStream(formDataBodyPart.getEntityAs(InputStream.class), out);
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart(
formDataBodyPart.getName(),
in,
cd.getFileName(),
formDataBodyPart.getMediaType()
);
streamDataBodyPart.getHeaders().putAll(formDataBodyPart.getHeaders());
copiedMultipart.bodyPart(streamDataBodyPart);
} else {
FormDataBodyPart fdbp = (FormDataBodyPart) bodyPart;
FormDataBodyPart formDataBodyPart = new FormDataBodyPart(fdbp.getName(), fdbp.getEntity(), fdbp.getMediaType());
copiedMultipart.bodyPart(formDataBodyPart);
}
}
return copiedMultipart;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment