简体   繁体   English

域类中的图像属性

[英]Image property in domain class

i tried to add img field on my domain class, i run my app everyting is ok. 我试图在域类上添加img字段,我运行我的应用程序,一切正常。 When i try to add img i get this error, 当我尝试添加img时出现此错误,

Property carImg is type-mismatched 属性carImg类型不匹配

here is my domain-class 这是我的领域课程

package carrentco


class Car {

    String brand
    String model
    String fuelType
    BigDecimal pricePerDay
    byte[] carImg


    static constraints = {
        brand(inList:["AUDI", "BMW", "MERCEDES", "NISSAN", "HONDA", "FORD"])
        model()
        fuelType(inList:["FUEL", "DIESEL", "AUTOGAS"])
        pricePerDay(min:0.0, max:1000.0)
        carImg(nullable:true, maxSize:1000000)
    }

}

here is what i add to my controller. 这是我添加到控制器中的内容。

def displayCarImg = {
        def car = Car.get(params.id)
        response.contentType = "image/jpeg"
        response.contentLength = car?.carImg.length
        response.outputStream.write(car?.carImg)
    }

and here is my show.gsp 这是我的show.gsp

<img src="${createLink(action:'displayCarImg', id:carInstance?.id)}" />

something similar here 这里有些相似

def viewPic(Long picId) {
        def photo = ChatUserPics.get( picId ?: params.id)
        if (photo) {
            byte[] image = photo.photo
            response.outputStream << image
        }
    }

Appears to have worked in that scenario, maybe you haven't followed any official documentations on all this.. 似乎在这种情况下可行,也许您还没有遵循所有官方文档。

I would highly recommend comparing storing images on file system vs db as a solution before rushing ahead. 我强烈建议先比较一下将图像存储在文件系统和数据库上,以作为解决方案。 Personally using DB as a pointer - file system location. 个人使用DB作为指针-文件系统位置。

If you have 100,000 users each having 10 images maybe most likely inexperienced and uploading 2mb files straight from phone camera. 如果您有100,000个用户,每个用户都有10张图像,则很可能没有经验,直接从手机摄像头上传2mb文件。 Now imagine backup time of that DB. 现在想象一下该数据库的备份时间。 VS migration costs in time if data needed replicating. 如果需要复制数据,则VS迁移会花费时间。 DB issues and having to rely on gig db backups to restore.. 数据库问题,必须依靠gig db备份来还原。

Anyways you are holding the steering wheel and driving am a passenger and think it may be a bumpy road 无论如何,您正握着方向盘驾驶着一个乘客,认为这可能是一条崎bump不平的道路

If the image is small enough, like avatar or thumb, you can store it as a Base64-encoded String: 如果图像足够小(例如头像或拇指),则可以将其存储为Base64编码的字符串:

class Car {    
    String brand
    String carImg
}

upon saving in a controller you do: 保存在控制器中后,您可以执行以下操作:

car.carImg = new String( imageAsByteArray ).encodeAsBase64()

and display it in a GSP: 并将其显示在GSP中:

<img src="${car.carImg}"/>

One big advantage of such approach is that you don't need to fetch each of your images in a new request. 这种方法的一大优势是,您无需在新请求中获取每个图像。 Instead you have a single data page with all embedded images. 相反,您只有一个包含所有嵌入图像的数据页。 This is extremely advantageous for mobile apps. 这对于移动应用程序极为有利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM