This repository was archived by the owner on Apr 8, 2026. It is now read-only.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.8k
Google App Engine Java
Joramatro edited this page Apr 16, 2013
·
24 revisions
Code samples to integrate the plugin with Google App Engine Java.
Contributed by yamsellem.
<divid="fileupload"><formmethod="post" enctype="multipart/form-data"><divclass="fileupload-buttonbar"><labelclass="fileinput-button"><span>Upload</span><inputtype="file" name="files[]" multiple></label></div></form><divclass="fileupload-content"><tableclass="files"></table></div></div><scriptid="template-upload" type="text/x-jquery-tmpl">
...<!-- report all the templates from the jQuery Upload Sample code (link above) -->...
</script>- main.js:
$(function(){/* activate the plugin */$('#fileupload').fileupload({submit: function(e,data){var$this=$(this);debugger;$.getJSON('/rest/file/url?'+newDate().getTime(),function(result){data.url=result.url;$this.fileupload('send',data);});returnfalse;}});});/rest/file/urlreturns a url bound to GAE (/_ah/upload) with a callback to our multipart POST method,/rest/file, as specified here:/rest/filePOSTing save the file and redirect to/rest/file/meta/rest/file/metareturns a JSON response as specified here
@Path("/file")
publicclassFileResource {
privatefinalBlobstoreServiceblobstoreService = BlobstoreServiceFactory.getBlobstoreService();
privatefinalBlobInfoFactoryblobInfoFactory = newBlobInfoFactory();
/* step 1. get a unique url */@GET@Path("/url")
publicResponsegetCallbackUrl() {
/* this is /_ah/upload and it redirects to its given path */Stringurl = blobstoreService.createUploadUrl("/rest/file");
returnResponse.ok(newFileUrl(url)).build();
}
/* step 2. post a file */@POST@Consumes(MediaType.MULTIPART_FORM_DATA)
publicResponsepost(@ContextHttpServletRequestreq,
@ContextHttpServletResponseres) throwsIOException,
URISyntaxException {
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKeyblobKey = blobs.get("files[]");
BlobInfoinfo = blobInfoFactory.loadBlobInfo(blobKey);
Stringname = info.getFilename();
longsize = info.getSize();
Stringurl = "/rest/file/" + blobKey.getKeyString();
ImagesServiceimagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions.Builder.withBlobKey(blobKey).crop(true).imageSize(80);
intsizePreview = 80;
StringurlPreview = imagesService
.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey)
.crop(true).imageSize(sizePreview));
FileMetameta = newFileMeta(name, size, url, urlPreview);
List<FileMeta> metas = Lists.newArrayList(meta);
Entityentity = newEntity(metas);
returnResponse.ok(entity, MediaType.APPLICATION_JSON).build();
}
/* step 3. redirected to the meta info */@GET@Path("/{key}/meta")
publicResponseredirect(@PathParam("key") Stringkey) throwsIOException {
BlobKeyblobKey = newBlobKey(key);
BlobInfoinfo = blobInfoFactory.loadBlobInfo(blobKey);
Stringname = info.getFilename();
longsize = info.getSize();
Stringurl = "/rest/file/" + key;
ImagesServiceimagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions.Builder.withBlobKey(blobKey).crop(true).imageSize(80);
intsizePreview = 80;
StringurlPreview = imagesService
.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey)
.crop(true).imageSize(sizePreview));
FileMetameta = newFileMeta(name, size, url, urlPreview);
List<FileMeta> metas = Lists.newArrayList(meta);
Entityentity = newEntity(metas);
returnResponse.ok(entity,MediaType.APPLICATION_JSON).build();
}
/* step 4. download the file */@GET@Path("/{key}")
publicResponseserve(@PathParam("key") Stringkey, @ContextHttpServletResponseresponse) throwsIOException {
BlobKeyblobKey = newBlobKey(key);
finalBlobInfoblobInfo = blobInfoFactory.loadBlobInfo(blobKey);
response.setHeader("Content-Disposition", "attachment; filename=" + blobInfo.getFilename());
BlobstoreServiceFactory.getBlobstoreService().serve(blobKey, response);
returnResponse.ok().build();
}
/* step 5. delete the file */@DELETE@Path("/{key}")
publicResponsedelete(@PathParam("key") Stringkey) {
Statusstatus;
try {
blobstoreService.delete(newBlobKey(key));
status = Status.OK;
} catch (BlobstoreFailureExceptionbfe) {
status = Status.NOT_FOUND;
}
returnResponse.status(status).build();
}
}- the representations
@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)
publicclassFileUrl {
Stringurl;
publicFileUrl(Stringurl) {
this.url = url;
}
publicFileUrl() {
}
}
@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)
publicclassFileMeta {
Stringname;
longsize;
Stringurl;
Stringdelete_url; Stringdelete_type;
Stringthumbnail_url; publicFileMeta(Stringfilename, longsize, Stringurl, StringurlPreview) {
this.name = filename;
this.size = size;
this.url = url;
this.delete_url = url;
this.delete_type = "DELETE";
this.thumbnail_url = urlPreview;
}
publicFileMeta() {
}
}
@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)
publicclassEntity {
privateList<FileMeta> files;
publicEntity(List<FileMeta> files) {
this.files = files;
}
publicEntity() {
}
}- web.xml
<web-app>
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>- pom.xml
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.7</version>
</dependency>