Agreed it would be better to be able to return a stream directly rather than futzing with PrintWriter.
I'm guessing this won't completely do the trick for you, but in one application I've generated a file attachment without using the server's disk by creating a document, using a MIME stream to create an attachment, then redirecting to that document, which can get cleaned up after the fact.
This isn't my code, but shows some basics for generating an attachment as an element of a document.
Agreed it would be better to be able to return a stream directly rather than futzing with PrintWriter.
I'm guessing this won't completely do the trick for you, but in one application I've generated a file attachment without using the server's disk by creating a document, using a MIME stream to create an attachment, then redirecting to that document, which can get cleaned up after the fact.
This isn't my code, but shows some basics for generating an attachment as an element of a document.
{Link}