Application of uploading pictures and videos on JAVA

Java Image and Video Upload
Cloudinary provides an API for uploading images, videos and any other type of files to the cloud. Files uploaded to Cloudinary are safely stored in the cloud with secure backups and revision history. Cloudinary's API allows for secure uploads from your server, directly from your visitor's browser or mobile app, or via a remote public URL.
Cloudinary's Java SDK wraps Cloudinari's upload API and simplifies integration. Java methods can be used to easily upload Java images and videos to the cloud, and Java helper methods can be used to upload directly from the browser to Cloudinary.
This page describes common usage patterns for uploading Java images and videos with Cloudinary.
For details on all available upload options and parameters, see the Media Upload documentation and Upload Methods for the Upload API Reference.


Tip: Cloudinary's Upload widget provides an alternative to using the CloudinarySDK to add upload functionality to your application, eliminating the need to develop internal interactive upload functionality. The Upload widget is an interactive, feature-rich, easy-to-integrate UI that enables you to add Cloudinary upload support to your website. Widgets can be easily embedded into web applications with just a few lines of JavaScript code. See the Upload widget documentation for details.



Server-Side Upload
You can upload images, videos, or any other raw files to Cloudinary from your Java code. Uploads are done over HTTPS using a secure protocol based on your account's api_key and api_secret parameters.
 

Java Image Upload
The following Java method uploads an image to the cloud:

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

The first parameter is the file source, and the second parameter is a map<String, Object> of additional upload parameters. The result of this method call is the deserialized server response - again, a Map<String, Object>. A RuntimeException is thrown if there is a server error or HTTP error.
For example, to upload a local image file named my_image.jpg:

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, remote HTTP or HTTPS URL, whitelisted bucket (S3 or Google storage) URL, base64 data URI, or FTP URL. See File Source Options for details.
For a complete list of Upload method parameters, see the Upload method in the Upload API Reference.
 

Java Video Upload
You upload videos in exactly the same way as pictures. However, the Upload method only supports uploading files up to 100 MB. To upload larger videos, use the UploadLarge method, which uploads large files to the cloud in chunks.
The UploadLarge method has the same signature and options as the Upload method, with the addition of an optional ChunkSize parameter (20 MB by default).
The following example uploads a dog. The mp4s are converted to Cloudinary and stored in a two-level folder structure with a common ID dog_closeup. It also performs two eager transformations, resizing the video to a square and a small rectangle.

cloudinary.uploader().upload("dog.mp4", 
    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", "https://mysite.example.com/notify_endpoint"));

Upload Responses
By default, uploads are performed synchronously. Once complete, uploaded images are immediately available for conversion and delivery. You can also perform asynchronous uploads using the UploadAsync method. See Asynchronous API Methods for more information.
The upload call returns a JSON object with the following content:

{
    "public_id":"tquyfignx5bxcbsupr6a",
    "version":1375302801,
    "signature":"52ecf23eeb987b3b5a72fa4ade51b1c7a1426a97",
    "width":1920,
    "height":1200,
    "format":"jpg",
    "resource_type":"image",
    "created_at":"2017-07-31T20:33:21Z",
    "bytes":737633,
    "type":"upload",
    "url":
        "https://res.cloudinary.com/demo/image/upload/v1375302801/tquyfignx5bxcbsupr6a.jpg",
    "secure_url":
        "https://res.cloudinary.com/demo/image/upload/v1375302801/tquyfignx5bxcbsupr6a.jpg",
    "etag":"1adf8d2ad3954f6270d69860cb126b24"
}

Responses are automatically parsed and converted to Maps.
The response includes HTTP and HTTPS URLs for accessing the uploaded media asset, as well as additional information about the uploaded asset: public id, resource type, width and height, file format, file size in bytes, signature used to verify the response, etc.

Uploading directly from the browser
The upload example mentioned above allows server-side Java code to upload media assets to Cloudinary. In this flow, if you have a web form that allows users to upload images or videos, the data for the media file is first sent to your server before being uploaded to Cloudinary.
A more efficient and robust option is to allow users to upload images and videos in client-side code directly from the browser to Cloudinary, rather than through the server. This approach allows for faster uploads and a better user experience. It also reduces the load on the server and reduces the complexity of Java or JavaEE applications.
You can upload directly from the browser using signed or unsigned calls to the upload endpoint, as shown in the uploading multiple files using a form example.
For signed uploads from client-side code, the security signature must be generated in server-side Java code. You can use the apiSignRequest method to generate a SHA signature:

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

Guess you like

Origin blog.csdn.net/std7879/article/details/127682745