grails笔记------数据库存储图片的图片上传与图片显示

本文介绍了一种使用Groovy语言中的闭包来处理图像数据的方法,该方法可以将存储在数据库中的图像数据读取出来并在网页上显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

domain中用byte类型

自动生成的页面就可以上传图像文件至数据库

写个闭包来输出图像的数据流  

def showImage = {
        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        if(params.id){
        def custom = Custom.findByCusId(params.id )

        if(custom.cusImage==null){
            println('custom 没有图片')
            java.io.File file = new File(servletContext.getRealPath( "/images") +"/null.jpg")
            byte[] bytes= file.readBytes()
             out.write(bytes)
            out.close();
        } else {
             def image= custom.cusImage
                //println image
                byte[] bytes= image
                out.write(bytes)
                //javax.imageio.ImageIO.write(bytes, "PNG", out);
                out.close();
        }
        }else{
            java.io.File file = new File(servletContext.getRealPath( "/images") +"/null.jpg")
            byte[] bytes= file.readBytes()
            out.write(bytes)
            out.close();
            println   'custom 没有传ID'
        }

    }

页面上这样调用就可以看到了

<image src="${createLink(controller:"custom", action:"showImage",id:custom.cusId)}" height="150" width="150"/>

 

更多信息请点击文章来源:http://www.haohaonews.com/show.php?tid=17

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值