In this topic, you will learn how to use an HTML page to upload a video to your media library using Java and the Media Write API. A servlet class handles the HTML form page, taking the video name, description, and file, and passes them into the create_video method.
Note that the solution presented below depends on 3rd party libraries. It is offered as an example only, and is not supported by Brightcove.
By downloading and examining the example code, you will learn how to programmatically upload video with Java and the Media Write API and customize the application as needed.
The sample consists of two files:
UploadVideo servlet.To follow the code in this topic, you should be familiar with Java servlets and form handling.
In this topic, you will learn about:
UploadVideo servlet classThe 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 JAR files.
First, the servlet parses 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();
}
}
This servlet uses 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. By iterating through that list, you can separate the metadata fields from the file fields.
The next step encodes the metadata in JSON:
/* STEP 2.
Assemble the JSON params
*/
String json = "{\"method\":\"create_video\"" +
", \"params\":{" +
"\"token\":" + "12345asdfasdfa67898" + ", " +
"\"video\":" +
"{\"name\":\"" + videoName + "\", " +
"\"shortDescription\":\"" + videoDescription + "\"}, " +
"}}";
The important points to notice are as follows:
GET call.params contains an object of additional arguments for the call, like your token and video metadata object.name and shortDescription properties (both required) and optionally any other properties you can set, like tags, relatedLink, economics, etc.Lastly, the servlet takes the JSON string and the file data and uses POST to send the data to the Write API. Let's look at the structure of the call used to upload the video in 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 );
This part of the servlet uses a helper library to simplify making the HTTP request because Java's networking stack is very low level. With this library, you just need to specify an object of parameters and the URL to POST them to.
The important points to notice are as follows:
JSON-RPC type requestThe json string contains the metadata for this video (This is explained in more detail later.)videoFilename and videoFile are the files that are uploadedNow that you've defined the UploadVideo servlet class, you can put it to use in a simple HTML form. See the working example code.
In this sample, we define a POST action for the 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 you've seen, parses the input, sets up, and calls the create_video method.
If you are having problems getting your videos to upload, Brightcove Support is available to help. You can submit a case here. To make sure you get the fastest response possible, below is a list of what support will need to solve the problem.
After your case is submitted, you will receive an email confirmation from Brightcove Support. To provide additional information on your case to Brightcove Support, reply to the confirmation email.
Post new comment
Comments