You've said in the comments that
I could see some extra bytes added in the corrupted file.
... well there's your problem, rather than "flushing/closing".
What extra bytes? I wonder if it is the "Byte Order Mark". Read about it here https://stackoverflow.com/a/48749396/1847378 - this article is about how to overcome a file/input stream with a BOM that you don't want. That's the opposite problem, though.
Maybe the use of stream
is unhelpfully messing around with the stream. Just for a test at least, how about passing ByteArrayOutputStream()
to the the outputDocument(..)
and then passing byte[]
(from ByteArrayOutputStream.toByteArray()
) to the JAX RS Response
?