在grails中下载jasper文件

在grails中下载jasper文件,grails,download,Grails,Download,我有以下代码: response.contentType = "application/octet-stream" response.setContentLength(templateFile.bytes.length) response.setHeader("Content-disposition", "attachment;filename=${fileName}") IOUtils.copy(templateFile,response.outputStream) 其中templateFi

我有以下代码:

response.contentType = "application/octet-stream"
response.setContentLength(templateFile.bytes.length)
response.setHeader("Content-disposition", "attachment;filename=${fileName}")
IOUtils.copy(templateFile,response.outputStream)
其中templateFile是Jasper报告模板(*.jrxml)。 我总是得到net::ERR_FILE_NOT_FOUND。 对于不同的文件类型,这段代码可以很好地工作


我还尝试了内容类型“text/xml”,但结果是一样的。

尝试使用
response.setContentLength()
设置数据的长度。此外,您还可以进行调试,查看文件的字节是否在
response.outputStream

以下是对我有用的片段:

byte[] bytes = getBytesOfFile()
response.setContentType("application/octet-stream")
response.setContentLength(bytes.length)
response.outputStream << bytes
byte[]bytes=getBytesOfFile()
response.setContentType(“应用程序/八位字节流”)
response.setContentLength(bytes.length)

在outputStream上调用flush()后,response.outputStream问题已解决。运算符ContentLength已设置,字节在outputStream中,但浏览器仍会获取ERR\u FILE\u NOT\u FOUND error.hmm可能文件已损坏?您能在系统中打开此文件而没有错误吗?文件有效。如我所见-GrailsDispatcherServlet对从控制器返回的ModelAndView执行“渲染”,outputStream变为空。看起来我应该深入研究这个方法。在
IOUtils.copy()之后有什么内容吗?