Java Example: Upload Video

Applies to Roles
Developer
Version
Brightcove 3
Modules
Media API
Edition
Pro, Enterprise

This example application uses an HTML page to upload a video to your media library, using Java and the Media Write API. The HTML form page is backed up by a servlet class that takes the video name, description, and file, and passes them into the create_video method. The sample consists of two files:

  • UploadVideo.java, which defines a servlet class that takes input from a form and passes it to the Write API create_video method.
  • uploadVideoForm.html, which presents a form in which the user enters a video's name, description, and path. The form's submit button passes the input to the UploadVideo servlet.

Before you begin

You should be familiar with Java servlets and form handling.

The UploadVideo servlet class

The servlet class accepts the form submission and reformats the data into a JSON-RPC call to the Write API. This example uses the ClientHTTPRequest helper class from the ClientHTTP library by Vlad Patryshev and requires the Apache Commons commons-io and commons-fileupload JARs.

The first thing this servlet does is parse the incoming POST from the HTML form:

/* STEP 1. 
   Handle the incoming request from the client
 */
 
// Request parsing using the FileUpload lib from Jakarta Commons
// http://commons.apache.org/fileupload/
 
// Create a factory for disk-based file items  
DiskFileItemFactory factory = new DiskFileItemFactory();
 
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1000000000);
 
// Parse the request into a list of DiskFileItems
List items = upload.parseRequest(request);
 
// The fields we will need for the API request
String videoName = "";
String videoDescription = "";
File videoFile = null;
String videoFilename = "";
long videoMaxSize = 0;
 
// Iterate through the list of DiskFileItems
Iterator iter = items.iterator();  
while (iter.hasNext()) {
	DiskFileItem item = (DiskFileItem) iter.next();
 
	if (item.isFormField()) {
		
		if (item.getFieldName().equals("name")) {
			videoName = item.getString();
		} else if (item.getFieldName().equals("desc")) {
			videoDescription = item.getString();                        
		}
 
	} else {
		videoFile = item.getStoreLocation();
		videoFilename = item.getName();
		videoMaxSize = item.getSize();
 
	}
}

We're using a helper library from Jakarta Commons in this example for parsing the request; you can write your own or use one of the many open source libraries created for this purpose. This library takes a multipart post (the kind sent from upload forms), and stores them in a List "FileItems". Iterating through that list we can separate the metadata fields from the file fields.

In the next step, we JSON-encode our metadata:

/* STEP 2. 
Assemble the JSON params
*/
 
String json = "{\"method\":\"create_video\"" +
          ", \"params\":{" +
          "\"token\":" + "12345asdfasdfa67898" + ", " +
          "\"video\":" + 
                "{\"name\":\"" +  videoName + "\", " +
                "\"shortDescription\":\"" + videoDescription + "\"}, " +
 
          "}}";

The important points here are:

  • The method of the call is a JSON parameter, rather than on the query string, as in a GET call.
  • params contains an object of additional arguments for the call, like your token and video metadata object
  • Within the video metadata object, you specify the name and shortDescription (both required) and optionally any other settable property like tags, relatedLink, economics, etc.

Lastly, we take this JSON string and the file data and POST it to the Write API. Let's look at the structure of the call we'll use to upload our video, the last bit of code in the servlet:

// Define the url to the api
String targetURL = "http://api.brightcove.com/services/post";
            
// Create the params object required by...
Object[] params;
if(videoFile == null) {
  params = new Object[] { "JSON-RPC", json };
} else {
  params = new Object[] {
        "JSON-RPC", json,
        videoFilename, videoFile
  }; 
}              
            
// ... the ClientHTTPRequest helper class from the ClientHTTP library by Vlad Patryshev
// http://www.devx.com/Java/Article/17679/1954?pf=true

InputStream in = ClientHttpRequest.post( new java.net.URL(targetURL), params );

We're using a helper library to simplify making the HTTP request because Java's networking stack is very low level. With this library, we just need to specify an object of parameters and the URL to POST them to.

Here are the important points:

  • "JSON-RPC" is the kind of request we're making
  • json is a string containing the metadata for this video; more on that later.
  • videoFilename and videoFile are the files that we're uploading

Uploading using an HTML form

Now that we've defined our UploadVideo servlet class, we can put it to use in a simple HTML form. See the sample.

In this sample, we define a POST action for our UploadVideo servlet. The form also presents two input fields where you can specify the name and description of the video you're uploading, and a third input field where you can browse for the video file in your file system:

<FORM ENCTYPE="multipart/form-data" method="POST" 
  action="http://localhost:8080/Library_API_Test/uploadvideo">
  Video name: <INPUT TYPE="text" NAME="name" value="video name"/><br/>
  Description: <INPUT TYPE="text" NAME="desc" value="description"/><br/>
  File:	<INPUT TYPE="file" NAME="file" />
  <INPUT TYPE="submit" VALUE="Upload"/>
</FORM>

The form's submit button passes the form input to the UploadVideo servlet, which, as we've seen, parses the input and sets up and calls the create_video method.

Tags
sample, upload