java - GAE + Spring MVC : Saving image file to BlobStore -


i succesfully save image blob in datastore 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 datastore etc } 

and try save in blobstore , save blob key in datastore. posible? snippet 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 datastore etc } 

i learn this example. , got error :

must called blob upload callback request. 

stack trace :

java.lang.illegalstateexception: must called blob upload callback request. @ com.google.appengine.api.blobstore.blobstoreserviceimpl.getuploads(blobstoreserviceimpl.java:160) @ com.google.appengine.api.blobstore.blobstoreserviceimpl.getuploadedblobs(blobstoreserviceimpl.java:144) @ com.shop.controller.productcontroller.add(productcontroller.java:92) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597)     .... 

solution :

finally found need generate url in jsp :

<form:form action='<%=blobstoreservice.createuploadurl("/admin/product/add")%>' commandname="product" methodparam="post" enctype="multipart/form-data"> 

instead of saving blobkey, try saving string representation via

 string blobstr = blobkey.getkeystring(); 

string representations can serialized , saved in datastore. can construct blobkey instance string representation

 blobkey blobkey = new blobkey(blobstr); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -