Java image and video upload

Last updated: Nov-14-2022

Cloudinary provides an API for uploading images, videos, and any other kind of file to the cloud. Files uploaded to Cloudinary are stored safely in the cloud with secure backups and revision history. Cloudinary's APIs allow secure uploading from your servers, directly from your visitors' browsers or mobile applications, or fetched via remote public URLs.

Cloudinary's Java SDK wraps Cloudinary's upload API and simplifies the integration. Java methods are available for easily performing Java image and video uploads to the cloud and Java helper methods are available for uploading directly from a browser to Cloudinary.

This page covers common usage patterns for Java image and video upload with Cloudinary.

For details on all available upload options and parameters, see the Media upload documentation, and the upload method of the Upload API Reference.

Cloudinary's Upload widget provides an alternative to using a Cloudinary SDK to add upload functionality to your application, eliminating the need to develop in-house interactive upload capabilities. The upload widget is an interactive, feature rich, simple-to-integrate user interface that enables you to add Cloudinary upload support to your website. The widget can be easily embedded in your web application with just a few lines of JavaScript code. See the Upload widget documentation for detailed information.

Upload widget main screen

Server-side upload

You can upload images, videos, or any other raw file to Cloudinary from your Java code. Uploading is done over HTTPS using a secure protocol based on your product environment's api_key and api_secret parameters.

Java image upload

The following Java method uploads an image to the cloud:

Copy to clipboard
import com.cloudinary.Cloudinary;
Cloudinary cloudinary = new Cloudinary();
cloudinary.upload(fileRef, ObjectUtils.emptyMap());

The first parameter is the file source and the second one is a map Map<String,Object> of additional upload parameters. The result of this method call is the deserialized server response - again, Map<String,Object>. In case of a server error or an HTTP error a RuntimeException is thrown.

For example, uploading a local image file named my_image.jpg:

Copy to clipboard
File file = new File("my_image.jpg");
Map uploadResult = cloudinary.uploader().upload(file, ObjectUtils.emptyMap());

The file to upload can be specified as a local path, a remote HTTP or HTTPS URL, a whitelisted storage bucket (S3 or Google Storage) URL, a base64 data URI, or an FTP URL. For details, see File source options.

For a full list of the Upload method parameters, see the upload method in the Upload API reference.

Java video upload

You upload videos in exactly the same way as images. However, the Upload method supports uploading files up to 100 MB only. To upload larger videos, use the UploadLarge method, which uploads large files to the cloud in chunks.

The UploadLarge method has the identical signature and options as the Upload method, with the addition of an optional ChunkSize parameter (default 20 MB).

The following example uploads dog.mp4 to Cloudinary and stores it in a bi-level folder structure with the public ID dog_closeup. It also performs two eager transformations that resize the video to a square and a small rectangle.

Copy to clipboard
    ObjectUtils.asMap("resource_type", "video",
    "public_id", "myfolder/mysubfolder/dog_closeup",
    "eager", Arrays.asList(
        new EagerTransformation().width(300).height(300).crop("pad").audioCodec("none"),
        new EagerTransformation().width(160).height(100).crop("crop").gravity("south").audioCodec("none")),
    "eager_async", true,
    "eager_notification_url", ""));

Upload response

By default, uploading is performed synchronously. Once finished, the uploaded image is immediately available for transformation and delivery. You can also perform an asynchronous upload using the UploadAsync method. See Asynchronous API methods for more information.

An upload call returns a JSON object with content similar to the following:

Copy to clipboard

The response is automatically parsed and converted into a Map.

The response includes HTTP and HTTPS URLs for accessing the uploaded media asset as well as additional information regarding the uploaded asset: The Public ID, resource type, width and height, file format, file size in bytes, a signature for verifying the response and more.

Related topics

  • For more information on uploading media assets, see the Media upload documentation.
  • For details on all available upload parameters, see the upload method of the Upload API Reference.

Direct uploading from the browser

The upload samples mentioned above allows your server-side Java code to upload media assets to Cloudinary. In this flow, if you have a web form that allows your users to upload images or videos, the media file's data is first sent to your server and only then uploaded to Cloudinary.

A more efficient and powerful option is to allow your users to upload images and videos in your client-side code directly from the browser to Cloudinary instead of going through your servers. This method allows for faster uploading and better user experience. It also reduces load from your servers and reduces the complexity of your Java or Java EE applications.

You can upload directly from the browser using signed or unsigned calls to the upload endpoint, as shown in the Upload multiple files using a form examples.

For signed uploads from your client-side code, a secure signature must be generated in your server-side Java code. You can use the apiSignRequest method to generate SHA signatures:

Copy to clipboard
cloudinary.apiSignRequest(Map<String, Object> paramsToSign, String apiSecret);

Related topics

  • For more information on uploading media assets, see the Media upload documentation.
  • For details on all available upload parameters, see the upload method of the Upload API Reference.

✔️ Feedback sent!

Rate this page: