【发布时间】:2013-01-04 16:06:44
【问题描述】:
我正在努力将图像存储为 Mongo 中的字节数组。 我的域名很简单
class Book {
String title
String author
byte[] photo
String photoType
}
图像都低于 300kB,所以我首先会避免使用 GridFS。 持久化后,照片似乎存储为字符串(始终为 11 个字节)
db.book.find() {“_id”:NumberLong(15),“作者”:“”,“照片”:“[B@774dba87”,“photoType”:“图像/jpeg”,“标题”:“”,“版本”:0 }
我的控制器显示如下: def saveImage() {
def bookInstance
if(request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
CommonsMultipartFile file = (CommonsMultipartFile)multiRequest.getFile("photo");
params.photoType = file.getContentType()
print "nb bytes " +file.bytes.length //TODO
bookInstance = new Book(params)
bookInstance.photo=new byte[file.bytes.length]
bookInstance.photo = file.getBytes()
def okcontents = ['image/png', 'image/jpeg', 'image/gif']
if (! okcontents.contains(file.getContentType())) {
flash.message = "Photo must be one of: ${okcontents}"
render(view:'create', model:[bookInstance:bookInstance])
return;
}
log.info("File uploaded: " + bookInstance.photoType)
}
if (!bookInstance.save()) {
render(view:'create', model:[bookInstance:bookInstance])
return;
}
flash.message = "Book Photo (${bookInstance.photoType}, ${bookInstance.photo.size()} bytes) uploaded."
redirect(action: "show", id: bookInstance.id)
}
我正在使用带有 mongo 插件的 Grails 2.2...
提前感谢您的提示(顺便说一句,祝您 2013 年快乐!)
干杯 菲利普
【问题讨论】:
-
“所以我会首先避免使用 GridFS”...为什么?!
-
我只存储小图,GridFS好像不被插件支持,除非我记错了。
-
小图像与否,GridFS 是要走的路。我不是 Grails 用户,但我发现了这个 - github.com/sergeyy/Grails-plugin-mongodb-gridfs
标签: mongodb grails grails-plugin