My favorites | Sign in
Logo
          
People details
Project owners:
  tytung

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.

Notice: GoogleFileService v0.3 supports appengine-java-sdk-1.2.1 ~ 1.2.6 now.
Thanks Zhang Yu for fixing the bug of getGoogleFileById() to be compatible with the latest SDK.

Key Features (What is New?)

GoogleFileService 0.1

GoogleFileService 0.2

GoogleFileService 0.3

Installation

  1. Download the latest GoogleFileService Zip files of source codes.
  2. Locate GoogleFileService\war\WEB-INF\appengine-web.xml and modify the property 'upload.allowed-client-ip' value to be a list of IPs that can upload files via Apache HttpClient API (if required).
  3. Apply your account on Google App Engine for Java.
  4. Upload GoogleFileService source codes to Google App Engine for Java. (How to upload)
  5. Finally, you can try it on https://[your_app_id].appspot.com/

Getting Started

You 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>

1. Webpage's form

If you want to upload files via the webpage, please read as follows:

2. Apache HttpClient

If you want to upload files via other java application, please read as follows:

How It Works

With 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

  1. If we submit a large file of size over 10 MB, then we'll receive a HTTP Error 413 Request Entity Too Large (Your client issued a request that was too large) from Google App Engine.
  2. The uploading progress bar works on local development server, but not works on Google App Engine.

Questions or Suggestions?

Google Groups => GoogleFileService









Hosted by Google Code