Dear all,
I had for quite a while a problem to load images in the app dev blobstore.
The problem was on the following line : "Map<String, BlobKey> blobs
=blobstoreService
.getUploadedBlobs(req);"
In the javadoc, it is mentioned that the getUploadedBlobs is deprecated and
we should use "getUploads" instead but the getUploads is not available. The
error code I had was "Must be called from a blob upload callback request.".
The appengine sdk is appengine-api-1.0-sdk-1.6.3.jar
The problem is apparently only occurring on the dev server. However, this
creates a difficult situation where it is not possible to test using the
dev server. In order to get around this problem, I have used the AppEngine
file mechanism. You will find the code below. Just thought it could be
useful for someone.
--------------------------------------------------------------------------------
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import java.io.InputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UploadPropertyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private BlobstoreService blobstoreService =
BlobstoreServiceFactory.getBlobstoreService();
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
BlobKey blobKey=null;
try {
FileService fileService= FileServiceFactory.getFileService();
AppEngineFile file;
ServletFileUpload upload = new ServletFileUpload();
res.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Got a form field: " + item.getFieldName());
} else {
System.out.println("Got an uploaded file: " + item.getFieldName() + ",
name = " + item.getName());
// You now have the filename (item.getName() and the
// contents (which you can read from stream). Here we just
// print them back out to the servlet output stream, but you
// will probably want to do something more interesting (for
// example, wrap them in a Blob and commit them to the
// datastore).
String mimeType=item.getContentType();
file = fileService.createNewBlobFile(mimeType);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
ByteBuffer buf = ByteBuffer.wrap(buffer, 0, len);
writeChannel.write(buf);
//res.getOutputStream().write(buffer, 0, len);
}
writeChannel.closeFinally();
blobKey = fileService.getBlobKey(file);
System.out.println("Blob key:"+blobKey);
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String imageUrl = imagesService.getServingUrl(blobKey);
System.out.println("imageUrl : "+ imageUrl);
res.sendRedirect("/upload?imageUrl=" + imageUrl);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
String imageUrl = request.getParameter("imageUrl");
response.setHeader("Content-Type", "text/html");
response.getWriter().println(imageUrl);
}
}
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-appengine-java/-/wwlNsif5kEgJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.