我有一個直接寫入輸出流的操作。有時候,我得到以下錯誤:錯誤處理GroovyPageView:getOutputStream()已被調用此響應
Error processing GroovyPageView: getOutputStream() has already been called for this response
Caused by getOutputStream() has already been called for this response
這一個:
Executing action [getImage] of controller [buddyis.ItemController] caused exception: Runtime error executing action
Caused by Broken pipe
我該如何解決這些問題呢?下面列出了我使用的操作。
注:我使用Tomcat 7.0.42,如果這是很重要的!
def getImage() {
byte [] imageByteArray = // some image bytes
response.setHeader 'Content-disposition', "attachment; filename=\"${imageName}${imageExtension}\""
response.setContentType("image/pjpeg; charset=UTF-8")
response.contentLength = imageByteArray.size()
response.outputStream.write(imageByteArray)
response.outputStream.flush()
response.outputStream.close()
return
}
嘗試呈現的東西(假),以指數或呈現狀態代碼,看看你仍然可以得到這個問題。在返回之前,你可以從'render(status:200)'或者至少'render'Done''開始。 – dmahapatro
爲什麼要這樣做? – confile
請參閱[此問題](http://stackoverflow.com/questions/13184603/grails-error-using-httpservletresponse-to-export-file-for-download)。 – dmahapatro