Grails 2.4.4 显示已存储在 MySQL 中的图像文件

Grails 2.4.4 Display an image file that has been stored in MySQL

我正在编写一个 grails (v2.4.4) 应用程序来存储音乐库中的歌曲信息。一首歌的 "attributes" 之一是 "key"。所以我想写一个 validation/lookup table (域 class)来保存所有有效的密钥。由于不是每个进行数据输入的人都会自动知道带有 1 个降号的五线谱是 F 大调 scale/key 我想包括一张五线谱的图像作为比较

(不能贴员工图,不够"points",有兴趣的可以去维基百科看员工图。http://en.wikipedia.org/wiki/F_major)

我可以将此文件作为 BLOB 存储到 MySQL table 中 - grails 做得很好。但是我终其一生(使用我发现的许多示例)都无法将图像获取到 "show"。我在 "Valid Key List" 中看到的只是 "bytes" 的字符串。有人能给我指出正确的方向吗?

有效密钥域 Class:

package musicdb

class ValidKeys {

    String  musicalKey
    String  aka
    byte[]  staffImg

    static constraints = {
        musicalKey (unique: true, nullable: false)
        aka (nullable: true)
        staffImg (nullable: true, maxSize: (1024*1024))
    }
}

ValidKeysControler:(标准 "generated" 控制器)

package musicdb

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class ValidKeysController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond ValidKeys.list(params), model:[validKeysInstanceCount: ValidKeys.count()]
    }

    def show(ValidKeys validKeysInstance) {
        respond validKeysInstance
    }

    def create() {
        respond new ValidKeys(params)
    }

    @Transactional
    def save(ValidKeys validKeysInstance) {
        if (validKeysInstance == null) {
            notFound()
            return
        }

        if (validKeysInstance.hasErrors()) {
            respond validKeysInstance.errors, view:'create'
            return
        }

        validKeysInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'validKeys.label', default: 'ValidKeys'), validKeysInstance.id])
                redirect validKeysInstance
            }
            '*' { respond validKeysInstance, [status: CREATED] }
        }
    }

    def edit(ValidKeys validKeysInstance) {
        respond validKeysInstance
    }

    @Transactional
    def update(ValidKeys validKeysInstance) {
        if (validKeysInstance == null) {
            notFound()
            return
        }

        if (validKeysInstance.hasErrors()) {
            respond validKeysInstance.errors, view:'edit'
            return
        }

        validKeysInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'ValidKeys.label', default: 'ValidKeys'), validKeysInstance.id])
                redirect validKeysInstance
            }
            '*'{ respond validKeysInstance, [status: OK] }
        }
    }

    @Transactional
    def delete(ValidKeys validKeysInstance) {

        if (validKeysInstance == null) {
            notFound()
            return
        }

        validKeysInstance.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'ValidKeys.label', default: 'ValidKeys'), validKeysInstance.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'validKeys.label', default: 'ValidKeys'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}

你快到了。在您在评论中提到的 displayStaffImage() 方法中,您还需要将图像的 MIME 类型作为内容类型。

例如:

def displayStaffImage() { 
    def validKeyInstance = File.get(params.id) 
    response.setContentType("image/png")
    response.outputStream << validKeyInstance.staffImg
    response.outputStream.flush() 
}

请参阅 Grails: displaying created image in gsp 接受的答案以获取另一个示例。

如果您不知道或不存储 MIME 类型,无论出于何种原因,您可以使用内容类型 "application/octet-stream"(表示二进制文件)并改为指定文件名:

def displayStaffImage() { 
    def validKeyInstance = File.get(params.id) 
    response.setContentType("application/octet-stream")
    response.setHeader("Content-disposition", "attachment;filename=\"${validKeyInstance.fileName}\"")
    response.outputStream << validKeyInstance.staffImg
    response.outputStream.flush() 
}

编辑:我注意到您实际上并没有在密钥描述中存储文件名,所以我的第二个建议可能不那么相关,但如果您有可用的信息,它仍然是一个可行的选择。

谢谢克里斯。

实际上我刚刚在 "show.gsp" 中实现了这个功能。我不必添加 "contentType" 来使其工作,但我想我会返回并添加该域字段和适当的代码,因为我发现的大多数示例似乎都包含它。

我为让它发挥作用所做的是:

  1. 控制器中 "def displayStaffImage()
    • 将 File.get(params.id) 更改为 ValidKeys.findById(params.id)
  2. 在“show.gsp”中使用了标签
    • <span class="property-value" aria-labelledby="staffImg-label"><img src="${createLink(controller:'ValidKeys', action:'displayStaffImage', id:"${validKeysInstance.id}") }" width='100'/></span>
    • 顺便说一句,"span" 部分是从 "generate" 步骤的另一个 "fields" 克隆而来的。这不是真的必要,但我想我会尝试并保持一致。

然而... 这在 "index.gsp" 中不起作用。 Id 不喜欢 "id" 在这里作为参数。我会尝试更多的想法,如果成功的话 post。但是,如果有人对在 "index.gsp" 中显示实际图像有任何指示,我将不胜感激。

再次感谢克里斯!

这对我有用 我在域

中有此列类型
class User implements Serializable {
    Blob userPic
}

我得到了结果

 def obj = User.findById(userId)
        def img= obj.userPic
        out << '<img src="data:image/png;base64,'
        out<< img.binaryStream.bytes.encodeBase64()
        out<< '">'