我在grails 2.2.4中使用jasper report插件来生成PDF文件。我的 Controller 代码:
import org.codehaus.groovy.grails.plugins.jasper.JasperExportFormat
import org.codehaus.groovy.grails.plugins.jasper.JasperReportDef
class LabController {
def jasperService
def directpdf() {
def reportDef = new JasperReportDef(name:'mbarang.jrxml', fileFormat:JasperExportFormat.PDF_FORMAT)
response.contentType = 'application/pdf'
response.outputStream << jasperService.generateReport(reportDef).toByteArray()
return(false);
}
}
这些代码在grails 2.2.4中正常工作。但是当我在grails 2.4.2上运行时,出现此错误:
Error 500: Internal Server Error
Message
getOutputStream() has already been called for this response
为什么我得到这个错误?
最佳答案
您应该考虑做几件事,以避免响应/输出流出现问题。
首先,您应该考虑刷新输出流:
response.outputStream.flush()
其次,您应该关闭输出流:
response.outputStream.close()
最后,您应该返回
null
以向Grails指示您不希望呈现 View 。return null
以上更改将确保在 Controller 方法中正确处理输出流。
关于grails - Jasper Report插件在Grails 2.4.2中生成PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24771449/