Skip to content

Instantly share code, notes, and snippets.

@christiangoudreau
Created August 30, 2011 00:27
Show Gist options
  • Save christiangoudreau/1179784 to your computer and use it in GitHub Desktop.
Save christiangoudreau/1179784 to your computer and use it in GitHub Desktop.
BlobStoreFiles
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.print("uploading files");
FileService fileService = FileServiceFactory.getFileService();
try {
ServletFileUpload upload = new ServletFileUpload();
res.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (!item.isFormField()) {
boolean lock = false;
InputStream stream = item.openStream();
out.print(item.getName());
AppEngineFile file = fileService.createNewBlobFile("image/png");
lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
ByteBuffer content = ByteBuffer.wrap(IOUtils.toByteArray(stream));
IOUtils.closeQuietly(stream);
writeChannel.write(content);
writeChannel.closeFinally();
lock = false;
BlobKey blobKey = fileService.getBlobKey(file);
if (blobKey == null) {
out.print("error while uploading");
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment