View Full Version : Image displaying issue.

17-12-2011, 08:21 PM
Hi. I have an issue displaying an image.
I debugged it and I saw a strange thing: in save() action file size is 10172 bytes, but when I retrieve it from database(showImage() action) it's size is 20343. It's almost twice largest!
Maybe somebody can help me. Please, look at my code.
I have a domain class Image:

class Image {
byte[] file;
String name;
String alt;
String title;
String contentType;
static constraints = {
name(nullable: false, blank: false)
file(nullable: false, maxSize: 1024 * 1024 * 2)
contentType(nullable: false, validator: {val, obj ->
return allowedContentTypes.contains(val);
static allowedContentTypes = ['image/png', 'image/jpeg', 'image/gif']

I save this image in ImageController in the following way:

def save() {
def image = new Image(params)
def file = ((MultipartRequest)request).getFile('file')
image.contentType = file.getContentType()
image.file = file.getBytes()
if (!image.save(flush: true)) {
render(view: "create", model: [image: image])

flash.message = message(code: 'default.created.message', args: [message(code: 'image.label', default: 'Image'), image.id])
redirect(action: "edit", id: image.id)

I have next action in controller to display an image:

def showImage() {
def image = Image.get(params.id)
response.contentType = image.contentType
response.contentLength = image.file.size()
OutputStream out = response.getOutputStream();

And finally I retrieve an image from my GSP like this:

<img src="${createLink(controller: 'image', action: 'showImage', id: image.id)}"/></g:link>

17-12-2011, 11:45 PM
I found the reason! It's my database! I'm using postgresql 8.4 and I have next line in my BuildConfig.groovy:

runtime 'postgresql:postgresql:8.3-603.jdbc4'
When I changed to H2 database everything become ok and images display well.
Can anybody say how to fix it ? Cause I really need postgresql.

17-12-2011, 11:50 PM
I found the reason. The problem was in jdbc driver version.