0

我正在尝试在 grails 中编写一个简单的包装器,它将从非公共后端 Web 服务器读取二进制文件,并将其作为流提供给公共服务器。第一个问题是处理这种解决方案的最佳方法是什么,因为 Grails 将尝试默认呈现视图。

我正在尝试一种导致“尝试从封闭流中读取”的解决方案。这显然是为了通过调用响应来避免视图渲染,但我不确定这是否是一个好的解决方案。

我正在通过以下代码读取后端文件:

def http = new HTTPBuilder(baseUrl)
http.request(method, ContentType.BINARY) {
                uri.path = path
                uri.query = query
                response.success = { resp, inputstream ->
                    log.info "response status: ${resp.statusLine}"
                    resp.headers.each { h -> log.info " ${h.name} : ${h.value}" }
                    return inputstream
                }

然后尝试通过控制器中的以下方法将其推送到客户端:

def binary(){
      response.outputStream << getBinaryContent("http://aaa" );
      response.flush()
    }

错误:“尝试从关闭的流中读取。”

4

1 回答 1

1

如果我没记错<<的话会关闭响应所以response.flush()是无效的。

这是我用于 CSV 的片段,只需替换为您的内容类型:

byte[] bytes = fileGenerated.getBytes() //fileGenerated is a File
response.setContentType("text/csv")
response.setContentLength(bytes.length)
response.setHeader("Content-Disposition", "attachment; filename=" + fileGenerated.getName())
response.outputStream << bytes
于 2013-04-03T19:38:09.903 回答