Created
August 30, 2011 00:27
-
-
Save christiangoudreau/1179784 to your computer and use it in GitHub Desktop.
BlobStoreFiles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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