GAE + Spring MVC : Saving image file to BlobStore

1 view
Skip to first unread message

Daniel Robertus via StackOverflow

unread,
May 21, 2013, 5:36:25 AM5/21/13
to google-appengin...@googlegroups.com

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)
    ....


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/16666630/gae-spring-mvc-saving-image-file-to-blobstore

skywalker via StackOverflow

unread,
May 21, 2013, 6:56:38 AM5/21/13
to google-appengin...@googlegroups.com

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);


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/16666630/gae-spring-mvc-saving-image-file-to-blobstore/16668273#16668273
Reply all
Reply to author
Forward
0 new messages