我想使用 grails 上的 groovy 上传图像。
我的gsp页面如下(我显示的是原始的简化版本)
<g:form controller="post" action="save" enctype="multipart/form-data">
My picture <input type="file" name="myPicture" />
<g:submitButton name="submit" value="Save"/>
</g:form>
我的域类如下:
class Post {
byte[] myPicture
static mapping = {
myPicture type: "blob"
}
我需要这个映射,否则 MySql 将创建一个小斑点,它太小而无法容纳图像
static constraints = {
myPicture(nullable:false)
}
}
在控制器上,我有一个名为“保存”的操作,如下所示:
def save = {
def post = loadPost(params.id)
post.properties = params
if(post.save()) {
print "hallo world"
redirect(action:'list', params:params)
} else {
render(view:'edit', model:[post:post])
}
}
当我尝试将图像保存在数据库中时引发异常。
2009-04-27 18:16:07,319 [20806951@qtp0-0] ERROR errors.GrailsExceptionResolver - java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
org.codehaus.groovy.runtime.InvokerInitationException:java.lang.ClassCastException:[B 无法转换为 java.sql.Blob
有任何提示这是为什么吗?
顺便说一句,我在教程中看到图像被作为字符串处理,但它不起作用
以及。