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