i succesfully save an image to Blob in Datastore with this code :
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(ModelMap model, Product form, HttpServletRequest request) {
if(ServletFileUpload.isMultipartContent(request)) { // if(form.getImage().getBytes().getSize() != 0) {
try {
Blob file = new Blob(form.getImage().getBytes());
form.setImg(file);
} catch (IOException e) {
e.printStackTrace();
}
}
//save to datastore etc
}
And i just try to save it in BlobStore and just save the Blob key in datastore. is it posible? this is my snippet but no works :
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(ModelMap model, Product form, HttpServletRequest request) {
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
BlobKey blobKey = blobs.get("image");
form.setImage1(blobKey);
//save to datastore etc
}
i learn from this example. and i got this error :
Must be called from a blob upload callback request.
Stack Trace :
java.lang.IllegalStateException: Must be called from a blob upload callback request.
at com.google.appengine.api.blobstore.BlobstoreServiceImpl.getUploads(BlobstoreServiceImpl.java:160)
at com.google.appengine.api.blobstore.BlobstoreServiceImpl.getUploadedBlobs(BlobstoreServiceImpl.java:144)
at com.shop.controller.ProductController.add(ProductController.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
....
instead of saving blobkey, try saving its string representation via
String blobStr = blobKey.getKeyString();
String representations can be easily serialized and saved in DataStore. You can always construct back BlobKey instance from their String representation
BlobKey blobKey = new BlobKey(blobStr);