Reputation: 685
i wanted to save an image in byte array.. and save into mongoDb Database.. and get back byte Array into image file and display on to .GSP page
Domain
class Profile{
static mapWith = "mongo"
String firstname
String lastname
byte[] imgpath
}
Controller
def saveimage{
File filepath = new File("C:\\man-of-steel-theme.jpg");
def encodedData = filepath.bytes;
profile.imgpath=encodedData;
profile.save();
}
In this am not sure is correct byte array save into mongodb and not able to get image file
Upvotes: 0
Views: 2815
Reputation: 17906
maybe this can be helpfull for you
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
}
Upvotes: 1