`
raodun
  • 浏览: 52424 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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

阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics