In order to automate file conversion we need some api’s in order to code your way out with some minimal code currently we are using zamzar api to convert around 1200 different types of files using the java code and Apache HTTP server.
There will be a free developer account you need to run on the cloud by exporting the files to their cloud and the code will automatically download it from their server.
First up you need to obtain Api key in order to get started with the conversion
You can also find this code on their website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | // import Apache HTTP Client v 4.3 import org.apache.http.*; import org.apache.http.auth.*; import org.apache.http.client.*; import org.apache.http.client.methods.*; import org.apache.http.impl.client.*; import org.apache.http.util.*; // import JSON import org.json.*; public class Format { public static void main(String[] args) throws Exception { String apiKey = "GiVUYsF4A8ssq93FR48H"; String endpoint = "https://sandbox.zamzar.com/v1/formats/gif"; // Create HTTP client and request object CloseableHttpClient httpClient = getHttpClient(apiKey); HttpGet request = new HttpGet(endpoint); // Make request CloseableHttpResponse response = httpClient.execute(request); // Extract body from response HttpEntity responseContent = response.getEntity(); String result = EntityUtils.toString(responseContent, "UTF-8"); // Parse result as JSON JSONObject json = new JSONObject(result); // Print result System.out.println(json); // Finalise response and client response.close(); httpClient.close(); } // Creates a HTTP client object that always makes requests // that are signed with the specified API key via Basic Auth private static CloseableHttpClient getHttpClient(String apiKey) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(apiKey, "")); CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(credentialsProvider) .build(); return httpClient; } } |
The JSON response provides some information about the GIF file format, such as the target file formats which Zamzar can convert GIFs to.
They have provided with an example JSON response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | { "name" : "gif", "targets" : [ { "name" : "bmp", "credit_cost" : 1 }, { "name" : "jpg", "credit_cost" : 1 }, { "name" : "webp", "credit_cost" : 1 } ] } |
To Start the conversion you need to send the request to their server for them to create an endpoint it is better to keep the file size below 1MegaByte not because they are saying that but just to test it out at first whether it is working or not after the endpoint you have to send a key value pair of hash on which format is converted into which format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | // import Apache HTTP Client v 4.3 import org.apache.http.*; import org.apache.http.auth.*; import org.apache.http.client.*; import org.apache.http.client.methods.*; import org.apache.http.entity.*; import org.apache.http.entity.mime.*; import org.apache.http.entity.mime.content.*; import org.apache.http.impl.client.*; import org.apache.http.util.*; // import JSON import org.json.*; // import from JDK import java.io.*; public class StartJob { public static void main(String[] args) throws Exception { String apiKey = "GiVUYsF4A8ssq93FR48H"; String endpoint = "https://sandbox.zamzar.com/v1/jobs"; String sourceFile = "/tmp/portrait.gif"; String targetFormat = "png"; // Create HTTP client and request object CloseableHttpClient httpClient = getHttpClient(apiKey); HttpEntity requestContent = MultipartEntityBuilder.create() .addPart("source_file", new FileBody(new File(sourceFile))) .addPart("target_format", new StringBody(targetFormat, ContentType.TEXT_PLAIN)) .build(); HttpPost request = new HttpPost(endpoint); request.setEntity(requestContent); // Make request CloseableHttpResponse response = httpClient.execute(request); // Extract body from response HttpEntity responseContent = response.getEntity(); String result = EntityUtils.toString(responseContent, "UTF-8"); // Parse result as JSON JSONObject json = new JSONObject(result); // Print result System.out.println(json); // Finalise response and client response.close(); httpClient.close(); } // Creates a HTTP client object that always makes requests // that are signed with the specified API key via Basic Auth private static CloseableHttpClient getHttpClient(String apiKey) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(apiKey, "")); CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(credentialsProvider) .build(); return httpClient; } } |
There will be status showing you whether it is initialising or it will throw an exception.
Here is an Example JSON Response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | { "id" : 15, "key" : "GiVUYsF4A8ssq93FR48H", "status" : "initialising", "sandbox" : true, "created_at" : "2013-10-27T13:41:00Z", "finished_at" : null, "source_file" : {"id":2,"name":"portrait.gif","size":90571}, "target_files" : [], "target_format" : "png", "credit_cost" : 1 } |
Zamzar api is now converting your file. Before you can access the result, you should check to see whether your job has finished successfully, by sending a GET request to the jobs/ID
endpoint. In this request, you’ll need to use the unique identifier returned by Zamzar when you created your job.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | // import Apache HTTP Client v 4.3 import org.apache.http.*; import org.apache.http.auth.*; import org.apache.http.client.*; import org.apache.http.client.methods.*; import org.apache.http.impl.client.*; import org.apache.http.util.*; // import JSON import org.json.*; public class Job { public static void main(String[] args) throws Exception { int jobId = 15; String apiKey = "GiVUYsF4A8ssq93FR48H"; String endpoint = "https://sandbox.zamzar.com/v1/jobs/" + jobId; // Create HTTP client and request object CloseableHttpClient httpClient = getHttpClient(apiKey); HttpGet request = new HttpGet(endpoint); // Make request CloseableHttpResponse response = httpClient.execute(request); // Extract body from response HttpEntity responseContent = response.getEntity(); String result = EntityUtils.toString(responseContent, "UTF-8"); // Parse result as JSON JSONObject json = new JSONObject(result); // Print result System.out.println(json); // Finalise response and client response.close(); httpClient.close(); } // Creates a HTTP client object that always makes requests // that are signed with the specified API key via Basic Auth private static CloseableHttpClient getHttpClient(String apiKey) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(apiKey, "")); CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(credentialsProvider) .build(); return httpClient; } } |
Once the file conversion is success ful you will receive a JSON stating the file conversion is successful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | { "id" : 15, "key" : "GiVUYsF4A8ssq93FR48H", "status" : "successful", "sandbox" : true, "created_at" : "2013-10-27T13:41:00Z", "finished_at" : "2013-10-27T13:41:13Z", "source_file" : {"id":2,"name":"portrait.gif","size":90571}, "target_files" : [{"id":3,"name":"portrait.png","size":15311}], "target_format" : "png", "credit_cost" : 1 } |
Now finally download the file from the zamzar cloud and delete the file in their server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // import Apache HTTP Client v 4.3 import org.apache.http.*; import org.apache.http.auth.*; import org.apache.http.client.*; import org.apache.http.client.methods.*; import org.apache.http.impl.client.*; // import from JDK import java.io.*; public class DownloadFile { public static void main(String[] args) throws Exception { int fileId = 3 String apiKey = "GiVUYsF4A8ssq93FR48H"; String endpoint = "https://sandbox.zamzar.com/v1/files/" + fileId + "/content"; String localFilename = "/tmp/portrait.png"; // Create HTTP client and request object CloseableHttpClient httpClient = getHttpClient(apiKey); HttpGet request = new HttpGet(endpoint); // Make request CloseableHttpResponse response = httpClient.execute(request); // Extract body from response HttpEntity responseContent = response.getEntity(); // Save response content to file on local disk BufferedInputStream bis = new BufferedInputStream(responseContent.getContent()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFilename)); int inByte; while((inByte = bis.read()) != -1) { bos.write(inByte); } // Print success message System.out.println("File downloaded"); // Finalise response, client and streams response.close(); httpClient.close(); bos.close(); bis.close(); } // Creates a HTTP client object that always makes requests // that are signed with the specified API key via Basic Auth private static CloseableHttpClient getHttpClient(String apiKey) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(apiKey, "")); CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(credentialsProvider) .build(); return httpClient; } } |
This is the basic conversion of the file but you can know more in their developer page they also provide support to PHP,Python,C#,ruby etc.
You can know mmore from here : link
Take your time to comment on this article.