2010-05-11

File storage in the cloud

I have been playing with google app engine for a while now mainly with Granite Data Services and Flex, recently I decided to see if I could store files in the datastore as blobs.

I stared by searching on the web on how to save a file as a blob to the datastore after a bit of playing I managed to get it all working using JSP pages and the apache commons fileupload, using this little bit of code:
ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iter; iter = upload.getItemIterator(req); FileItemStream fileItem;
while(iter.hasNext() == true) {  fileItem = iter.next();  InputStream fileStream = fileItem.openStream();  Blob imageBlob = new Blob(IOUtils.toByteArray(fileStream));  if (fileItem.getContentType() != null)   FileDAL.createFile(fileItem.getName(), fileItem.getContentType(), imageBlob); }
The full source is available on github.

I have decided that I really hate JSP Pages so I am going to look into a different template engine stay tuned...

No comments:

Post a Comment