我想在字节数组中保存图像.. 并保存到mongoDb数据库..并将字节数组返回到图像文件中 并显示在.GSP页面
域
class Profile{
static mapWith = "mongo"
String firstname
String lastname
byte[] imgpath
}
控制器
def saveimage{
File filepath = new File("C:\\man-of-steel-theme.jpg");
def encodedData = filepath.bytes;
profile.imgpath=encodedData;
profile.save();
}
在此不确定是否正确的字节数组保存到mongodb并且无法获取图像文件
答案 0 :(得分:1)
也许这对你有帮助
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import javax.imageio.stream.ImageInputStream
import javax.imageio.stream.MemoryCacheImageInputStream
class xyzClass {
def zabcdef(){
org.springframework.web.multipart.commons.CommonsMultipartFile multipartfile = request.getFile('picture')
if (!multipartfile || multipartfile.getContentType() != 'image/jpeg') {
render("${message(code:'error.wrong.file.type')}: jpeg")
return;
}
ImageInputStream iis = new MemoryCacheImageInputStream(multipartfile.getInputStream())
BufferedImage image = ImageIO.read(iis)
storeImage(image,"foto")
}
}
private storeImage(BufferedImage image, String name) {
ByteArrayOutputStream os = new ByteArrayOutputStream()
ImageIO.write(image, "jpg", os)
byte[] buf = os.toByteArray()
InputStream is = new ByteArrayInputStream(buf)
//store
}