Reputation: 33297
My app has jpg image files that are displayed on my website.
in my GSP I do:
<img class="itemThumImage border-radius-top-3px" src="${createLink(controller:'item', action:'getImage', id:item.id)}" />
my getImage action of my ItemConroller is:
def getImage() {
def item = Item.get(params.id)
def file = new File(grailsApplication.parentContext.servletContext.getRealPath(item.id))
response.setContentType("image/jpeg")
response.setHeader("Content-disposition", "filename=\"${item.id}.jpg\"")
response.setContentLength(fileBytes.size())
response.outputStream << file.readBytes()
response.outputStream.close()
}
Is this a good way of sending the image back to the browser? I think that the image is loaded into heap. Can I accelerate this process somehow? Do I need close()
at the last line or is it flush()
or both?
Upvotes: 0
Views: 170
Reputation: 35961
That's good for development/testing (but don't forget to return null
after closing response).
But never do this on production system. You need a frontend server in front of your Tomcat, like Nginx or Apache HTTPD server.
I recommend Nginx. For Nginx you could use following configuration to serve this files:
location /item/getImage/ {
alias /path/to/directory/with/images;
}
location / {
//proxy to your Tomcat instance
proxy_pass http://127.0.0.1:8080;
}
Upvotes: 1