0

我有以下代码在 Grails 的输出流上发送 jpg 图像。我在缓存标头 generate{...} 语句中使用此块。

                            def imageByteArray = // some image byte []
            response.setContentType("image/jpeg")
            response.setHeader("Content-disposition", "filename=\"${imageName}${imageExtension}\"")
            response.setContentLength(imageByteArray.size())

            def outputStream = null
            try {
                outputStream = response.outputStream
                outputStream << imageByteArray
            } catch (IOException e){
                log.error('getImage() - Canceled download?', e)
            } finally {
                if (outputStream != null){
                    try {
                        outputStream.close()
                    } catch (IOException e) {
                        log.error('Exception on close', e)
                    }
                }
                return
            }

我收到以下错误:

2013-09-17 12:51:55,033 [http-nio-80-exec-15] ERROR test.ItemController  - getImage() - Canceled download?
ClientAbortException:  java.io.IOException: Broken pipe
    at test.ItemController$_getImage_closure2_closure11.doCall(ItemController.groovy:287)
    at com.grailsrocks.cacheheaders.CacheHeadersService.callClosure(CacheHeadersService.groovy:209)
    at com.grailsrocks.cacheheaders.CacheHeadersService.withCacheHeaders(CacheHeadersService.groovy:201)
    at CacheHeadersGrailsPlugin$_addCacheMethods_closure7_closure11.doCall(CacheHeadersGrailsPlugin.groovy:61)
    at test.ItemController.getImage(ItemController.groovy:204)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at test.ItemController.getFacebookImage(ItemController.groovy:98)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

我无法弄清楚要更改什么以避免此错误。我也尝试在输出流上使用 flush() 但它不起作用。该怎么办?

从错误日志中我可以看到“取消下载?” 被抛出,因此我认为写操作出了点问题?

4

0 回答 0