|
Project Information
Members
Featured
Downloads
Links
|
What is GoogleFileService?GoogleFileService is based on Google App Engine for Java, and its aim is to provide data models and APIs for uploading and downloading large files of each size is up to 10MB [1] to and from Google Datastore via HTTPS securely. Please visit the latest demo on Google App Engine or download the Zip files of source codes to develop or run it yourself. Key Features (What is New?)
Installation
Getting StartedYou can upload your file via the webpage's form or via Apache HttpClient API. We handle uploaded file by using FileUploadServlet.java defined in web.xml as follows: <servlet>
<servlet-name>upload</servlet-name>
<servlet-class>sinica.googlefileservice.server.servlet.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<form action="/upload" method="post" enctype="multipart/form-data"> <input type="hidden" name="fileOwner" /> <div>FileId: <input type="text" name="fileId" size="50" /></div> <div>FileObject: <input type="file" name="upfile" size="50" /></div> <div><input type="submit" name="submit" value="Upload" /></div> </form>
// Google App Engine URL String url = "https://[your_app_id].appspot.com/upload"; // Create an instance of FileServiceClient for Google App Engine FileServiceClient client = FileServiceClientFactory.getFileServiceClient(url); // submit(String fileId, String fileOwner, String fileName, byte[] fileData, String contentType) int fileSize = client.submit(fileId, fileOwner, fileName, fileData, contentType); How It WorksWith GoogleFile and GoogleUnit data models (datastore), you can upload a large file of size up to 10 MB very easily via our static method DatastoreUtils.insertGoogleFile(). Some code of FileUploadServlet.java is as follows: String fileId = "";
String fileOwner = "";
String fileName = "";
int fileSize = -1;
String contentType = "";
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Set overall request size constraint: the default value of -1 indicates that there is no limit.
upload.setSizeMax(10240000); //10MB, the maximum allowed size, in bytes.
// Set the UTF-8 encoding to grab the correct uploaded filename, especially for Chinese
upload.setHeaderEncoding("UTF-8");
// Parse the request
FileItemIterator iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
InputStream stream = item.openStream();
String fieldName = item.getFieldName();
if (item.isFormField()) {
//process a regular form field
if (fieldName.equals("fileId"))
//set the UTF-8 encoding to grab the correct string
fileId = Streams.asString(stream, "UTF-8");
if (fieldName.equals("fileOwner"))
//set the UTF-8 encoding to grab the correct string
fileOwner = Streams.asString(stream, "UTF-8");
} else {
//process a file upload
fileName = item.getName();
if (fileName != null)
fileName= FilenameUtils.getName(fileName);
contentType = item.getContentType();
if (fieldName.equals("upfile")) {
// Check if the fileId conforms to the Key format of the Google datastore
// and all other uploaded fields are not empty.
if (DatastoreUtils.isKey(fileId) && fileOwner.length() > 0 && fileName.length() > 0) {
// Save into Google datastore
fileSize = DatastoreUtils.insertGoogleFile(fileId, fileOwner, fileName, contentType, stream);
}
}
}
}Known Issues
Questions or Suggestions?
|
