Created
August 20, 2013 03:18
-
-
Save uris77/6276753 to your computer and use it in GitHub Desktop.
Rendering Image in Grails that lives in the file system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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