Last active
August 29, 2015 14:09
-
-
Save narup/975b9f6c58e4bc5c41db to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.servlet; | |
| import java.io.IOException; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| import javax.servlet.http.HttpServlet; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| 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.blobstore.UploadOptions; | |
| import com.google.appengine.api.images.ImagesServiceFactory; | |
| import com.google.appengine.api.images.ImagesServiceFailureException; | |
| import com.google.appengine.api.images.ServingUrlOptions; | |
| public class UploadHandler extends HttpServlet { | |
| private static final String BUCKET_NAME = "[YOUR_BUCKET_NAME]"; | |
| private static final String JSON_CONTENT_TYPE = "application/json; charset=UTF-8"; | |
| private static final Logger logger = Logger.getLogger(Upload.class.getName()); | |
| @Override | |
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | |
| BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); | |
| UploadOptions uploadOptions = null; | |
| String environment = System.getProperty("com.google.appengine.runtime.environment"); | |
| String bucket = null; | |
| if ("Production".equalsIgnoreCase(environment)) { | |
| bucket = BUCKET_NAME; | |
| } | |
| if (bucket == null || bucket.isEmpty()) { | |
| uploadOptions = UploadOptions.Builder.withDefaults(); | |
| } else { | |
| uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName(bucket); | |
| } | |
| String uploadUrl = blobstoreService.createUploadUrl("/upload", uploadOptions); | |
| String successJSON = "{\"success\":\"true\", \"data\":\"" + uploadUrl + "\"}"; | |
| response.setContentType(JSON_CONTENT_TYPE); | |
| try { | |
| response.getWriter().write(successJSON); | |
| } catch (IOException e) { | |
| logger.log(Level.SEVERE, "Error sending response", e); | |
| } | |
| } | |
| @Override | |
| public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { | |
| BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); | |
| Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req); | |
| for (String key : blobs.keySet()) { | |
| logger.info("BLOB KEY:" + key); | |
| } | |
| List<BlobKey> keys = blobs.get("userfile"); | |
| if (keys != null && keys.size() > 0) { | |
| String title = req.getParameter("filename"); | |
| BlobKey blobKey = keys.get(0); | |
| //this is response data to client you can have any other format! | |
| String successJSON = "{\"success\":\"true\",\"filename\":\"" + title + "\", \"mainUrl\":\"" + getPhotoDisplayUrl(blobKey) | |
| + "\", \"secondaryUrl\":\"" + getThumbnailUrl(blobKey) + "\"}"; | |
| logger.info("UPLOAD RESPONSE JSON:" + successJSON); | |
| res.setContentType(JSON_CONTENT_TYPE); | |
| try { | |
| res.getWriter().write(successJSON); | |
| } catch (IOException e) { | |
| logger.log(Level.SEVERE, "Error sending response", e); | |
| } | |
| } else { | |
| logger.info("UPLOAD FAILED"); | |
| res.sendError(400, "Request cannot be handled."); | |
| } | |
| } | |
| /*Get thumbnail image of size 100x100 using blob key*/ | |
| public String getThumbnailUrl(BlobKey blobKey) { | |
| ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey); | |
| options.imageSize(100); | |
| options.crop(true); | |
| try { | |
| return ImagesServiceFactory.getImagesService().getServingUrl(options); | |
| } catch (ImagesServiceFailureException e) { | |
| logger.severe("Failed to get image serving url: " + e.getMessage()); | |
| return ""; | |
| } catch (Exception e) { | |
| logger.severe("Invalid blob key: " + e.getMessage()); | |
| return ""; | |
| } | |
| } | |
| public String getPhotoDisplayUrl(BlobKey blobKey) { | |
| ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey); | |
| options.imageSize(1024); | |
| options.crop(false); | |
| try { | |
| return ImagesServiceFactory.getImagesService().getServingUrl(options); | |
| } catch (ImagesServiceFailureException e) { | |
| logger.severe("Failed to get image serving url: " + e.getMessage()); | |
| return ""; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment