Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.tectonica.gae;
  
  import java.util.Date;
  import java.util.List;
  import java.util.Locale;
  import java.util.Map;
 
 
 
 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.FileInfo;
 import  com.google.appengine.api.images.ImagesService;
 import  com.google.appengine.api.images.ImagesServiceFactory;
 import  com.google.appengine.api.images.ServingUrlOptions;
A generic servlet, implementing GAE's blobstore protocol to upload and download static content. To use it, you need an entry in your web.xml file:
 <servlet>
    <servlet-name>Blobstore</servlet-name>
    <servlet-class>com.tectonica.gae.GaeBlobServlet</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>Blobstore</servlet-name>
    <url-pattern>/blob</url-pattern>
 </servlet-mapping>
 
Additionally, you need to create a API that would generate an upload link. This API should check permissions etc., and ultimately return:
 return BlobstoreServiceFactory.getBlobstoreService().createUploadUrl(GaeBlobServlet.BLOB_URI);
 
The link returned by the above code will take care of the actual upload and storage, and then forward the request to this servlet for analysis and generation of response (JSON in our implementation).

Author(s):
Zach Melamed
 
 public class GaeBlobServlet extends HttpServlet
 {
 	private static final long serialVersionUID = 1L;

taken from the web.xml servlet mapping for GaeBlobServlet
 
 	private static final String DEFAULT_BLOB_URI = "/blob";
 	private static String serveUrl = ;
 	private static String postUploadUri = ;
 
 	private static final BlobstoreService blobSvc = BlobstoreServiceFactory.getBlobstoreService();
 	private static final ImagesService imgSvc = ImagesServiceFactory.getImagesService();
 
 	public void init() throws ServletException
 	{
 		String postUploadUriParam = getServletConfig().getInitParameter("post-upload-uri");
 		if (postUploadUriParam != null)
 		{
 			 = postUploadUriParam;
 //			System.out.println("Overiden postUploadUri: " + postUploadUri);
 		}
 
 		String serveUrlParam = getServletConfig().getInitParameter("serve-url");
 		if (serveUrlParam != null)
 		{
 			 = serveUrlParam;
 //			System.out.println("Overiden serveUrl: " + serveUrl);
 		}
 	}

a callback handler, invoked by GAE after an upload ended successfully. we use it to analyze what was uploaded and return it to the sender in an organized JSON
 
 	{
 		Map<StringList<BlobKey>> uploads = .getUploads(req);
 		Map<StringList<FileInfo>> infos = .getFileInfos(req);
 
 //		System.out.println("doPost.req.getPathInfo=" + req.getPathInfo());
 
 		List<UploadRecuploadRecs = new ArrayList<>(uploads.size());
 		for (Entry<StringList<BlobKey>> entry : uploads.entrySet())
		{
			String fieldName = entry.getKey();
			BlobKey blobKey = entry.getValue().get(0);
			FileInfo info = infos.get(fieldName).get(0);
			String contentType = info.getContentType();
			final String servingUrl;
			if (contentType.startsWith("image/"))
				servingUrl = .getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
			else
				servingUrl = getServingUrl(req) + "?k=" + blobKey.getKeyString();
			UploadRec upload = new UploadRec();
			upload.setFieldName(fieldName);
			upload.setCreatedOn(new Date());
			upload.setUrl(servingUrl);
			upload.setFilename(info.getFilename());
			upload.setContentType(contentType);
			upload.setSize(info.getSize());
			uploadRecs.add(upload);
//			System.out.println(upload);
		}
		res.setContentType("application/json");
		res.getOutputStream().print(Jackson1.propsToJson(uploadRecs));
	}
	{
			return baseUrlOf(req) + ;
		return ;
	}

serves of a blob stored in the datastore, identified by its blob-key
	{
		.serve(new BlobKey(req.getParameter("k")), res);
	}
	private static String baseUrlOf(HttpServletRequest req)
	{
		String url = req.getRequestURL().toString();
		return url.substring(0, url.length() - req.getRequestURI().length()) + req.getContextPath();
	}
	public static String createUrl()
	{
		return .createUploadUrl();
	}
	{
	}
	public static boolean isMultipart(ServletRequest request)
	{
		String contentType = request.getContentType();
		if (contentType != null && contentType.toLowerCase(.).startsWith("multipart/form-data"))
			return true;
		return false;
	}
	// //////////////////////////////////////////////////////////////////////////////
	@JsonPropertyOrder({ "fieldName""createdBy""createdOn""url""filename""contentType""size" })
	public static class UploadRec
	{
		private String fieldName;
		private Date createdOn;
		private String url;
		private String filename;
		private long size;
		{
			return ;
		}
		public void setFieldName(String fieldName)
		{
			this. = fieldName;
		}
		public Date getCreatedOn()
		{
			return ;
		}
		public void setCreatedOn(Date createdOn)
		{
			this. = createdOn;
		}
		public String getUrl()
		{
			return ;
		}
		public void setUrl(String url)
		{
			this. = url;
		}
		{
			return ;
		}
		public void setFilename(String filename)
		{
			this. = filename;
		}
		{
			return ;
		}
		public void setContentType(String contentType)
		{
			this. = contentType;
		}
		public long getSize()
		{
			return ;
		}
		public void setSize(long size)
		{
			this. = size;
		}
		public String toString()
		{
			return "UploadRec [fieldName=" +  + ", createdOn=" +  + ", url=" +  + ", filename=" + 
", contentType=" +  + ", size=" +  + "]";
		}
	}
New to GrepCode? Check out our FAQ X