Use response.setContentLength instead of setting the header
Use the groovy '<<' operator to write to the steam, and there's no need to flush/close it