Skip to content

Instantly share code, notes, and snippets.

@uris77
Created August 20, 2013 03:18
Show Gist options
  • Save uris77/6276753 to your computer and use it in GitHub Desktop.
Save uris77/6276753 to your computer and use it in GitHub Desktop.
Rendering Image in Grails that lives in the file system
package org.uris.potholes.web.controllers
import javax.imageio.ImageIO
import java.awt.image.BufferedImage
class ImageDisplayController {
private final static String PICTURES_DIR = "/usr/local/code/grails/potholes/pictures"
def renderImage() {
String imageName = params.imageName
File imageFile = new File("${PICTURES_DIR}/${imageName}")
BufferedImage originalImage = ImageIO.read(imageFile)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
ImageIO.write(originalImage, "jpg", outputStream)
byte[] imageInByte = outputStream.toByteArray()
response.setHeader("Content-Length", imageInByte.length.toString())
response.contentType = "image/jpg"
response.outputStream << imageInByte
response.outputStream.flush()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment