Performance 使用流而不是静态图像文件服务来服务图像文件

Performance 使用流而不是静态图像文件服务来服务图像文件,performance,servlets,grails,Performance,Servlets,Grails,我有一个Grails web应用程序。目前我正在为我的用户提供静态图像文件。我在StackOverflow上看到了这篇文章,其中讨论了一种解决方案,即将图像作为流来提供,以隐藏其来源。我的问题是,这不是有一些折衷办法,比如在服务器上加载,因为它需要打开一个套接字才能将其作为静态文件流式传输给用户v.s // controller action def displayGraph = { def img // byte array //... response.setHead

我有一个Grails web应用程序。目前我正在为我的用户提供静态图像文件。我在StackOverflow上看到了这篇文章,其中讨论了一种解决方案,即将图像作为流来提供,以隐藏其来源。我的问题是,这不是有一些折衷办法,比如在服务器上加载,因为它需要打开一个套接字才能将其作为静态文件流式传输给用户v.s

// controller action
def displayGraph = {
    def img // byte array
    //...
    response.setHeader('Content-length', img.length)
    response.contentType = 'image/png' // or the appropriate image content type
    response.outputStream << img
    response.outputStream.flush()
}
//控制器操作
def displayGraph={
def img//字节数组
//...
response.setHeader('Content-length',img.length)
response.contentType='image/png'//或相应的图像内容类型

response.outputStream
response.outputStream谢谢。这样可以隐藏映像的源代码,也不会占用连接或套接字?没有性能警告?没有。在获取资源的性能方面,这种方式与服务静态映像没有区别。但是静态映像可以更好地缓存这是缺点如果我将用户配置文件的静态图像存储在web应用程序区域之外的某个位置,我是否仍然可以使用静态图像链接将其提供给用户,但不允许他们直接访问该目录?当然可以。但是如果您需要有关此方面的建议,您应该发布新问题。
<img src="${createLink(controller: 'myController', action: 'displayGraph')}"/>
response.outputStream << img
response.outputStream.flush()