Groovy中的类抛出异常 [英] Class cast exception in Groovy

查看:1006
本文介绍了Groovy中的类抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用grails上的groovy上传图片。
我的gsp页面如下(我正在展示原件的简化版本)

 < g:form controller =postaction =saveenctype =multipart / form-data> 
我的照片< input type =filename =myPicture/>
< g:submitButton name =submitvalue =Save/>
< / g:表格>

我的域名类如下:

 class Post {

byte [] myPicture

static mapping = {
myPicture类型:blob
}

我需要这个映射,否则MySql会创建一个smallblob, / p>

 静态约束= {
myPicture(可空值:false)
}

}

在控制器中,我有一个名为save的动作,如下所示:

  def save = {
def post = loadPost(params.id)

post.properties = params

if(post.save()){
printhallo world
redirect(action:'list',params:params)
} else {
渲染(view:'edit',model:[post:post])
}
}

当我尝试t时抛出异常将图像保存在数据库中。

  2009-04-27 18:16:07,319 [20806951 @ qtp0-0]错误errors.GrailsExceptionResolver  -  java.lang.ClassCastException:[B不能转换为java.sql.Blob 

org.codehaus.groovy.runtime.InvokerInvocationException:java.lang.ClassCastException:[B不能转换为java.sql.Blob



任何提示为什么会这样?



顺便说一句,我在一个教程中看到图像是作为字符串处理的,但它也不能用



  def save = {$ b解析方案


$ b def post = loadPost(params.id)

def f = request.getFile('myPicture')

post.myPicture = f.getBytes( )
post.pictureType = f.getContentType()


if(post.save()){


I want to upload an image using a groovy on grails. My gsp page is as follows (I am showing a simplified version of the original)

<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>

My domain class is as follows:

class Post {    

byte[] myPicture

static mapping = {
    myPicture type:  "blob" 
}

I need this mapping otherwise MySql will create a smallblob which is to small to fit the images

static constraints = {
    myPicture(nullable:false)      
}

}

At the controller I have an action called save which is as follows:

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])
    }
}

The exception is thrown when I try to save the image at the DB.

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.InvokerInvocationException: java.lang.ClassCastException: [B cannot be cast to java.sql.Blob

Any hint why is this?

BTW, I've seen in a tutorial that images were handled as strings but it didn't work as well.

解决方案

try this way:

  def save = {                               

    def post = loadPost(params.id)

    def f = request.getFile('myPicture')             

    post.myPicture = f.getBytes()
    post.pictureType = f.getContentType()                    


    if(post.save()) {

这篇关于Groovy中的类抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆