Java API Client

Java integration handles uploads and file operations by wrapping Uploadcare Upload API and REST API. This comprehensive API client lets you use most of the Uploadcare features from within your Java app.

GitHub →

Features

Uploading (Upload API):

  • Upload files from local storage and URLs (up to 5 TB)
  • Multipart uploading for large files
  • Uploading network to speed uploading jobs (like CDN)

File management (REST API):

  • Get file info and perform various operations (store/delete/copy) with them
  • Work with groups of files
  • Get info about account project
  • Manage webhooks
  • Paginated resources are fetched as List<T>
  • CDN path builder

Image processing (URL API):

  • Compression
  • Geometry
  • Colors
  • Definition
  • Image and text overlays
  • Rotations
  • Recognition
  • File info
  • Proxy (fetch)

Security features:

  • Secure authentication
  • Secure uploads (signed uploads)

Requirements

Go to your Uploadcare Dashboard and create a new project for your Java app. It’ll have API keys to complete Java API client integration.

Setup

The latest stable library version is available at Maven Central.

Include the following code into your build by adding the following dependencies into pom.xml for your project.

1<dependency>
2 <groupId>com.uploadcare</groupId>
3 <artifactId>uploadcare</artifactId>
4 <version>3.5.3</version>
5</dependency>

To build with Gradle add to Gradle build file:

1implementation 'com.uploadcare:uploadcare:3.5.3'

This guide covers uploadcare-java v3.5.3 which uses REST API v0.6. Support for REST API v0.7 (including getMetadata(), getContentInfo(), and getAppData()) is under development.

Usage example

Create Client and basic API usage

Use your Public and Secret API Keys from the Java project’s dashboard.

Create a new API Client:

1Client client = new Client("YOUR_PUBLIC_KEY", "YOUR_SECRET_KEY");
2Project project = client.getProject();
3Project.Collaborator owner = project.getOwner();
4
5List<URI> published = new ArrayList<URI>();
6Iterable<File> files = client.getFiles().asIterable();
7for (File file : files) {
8 if (file.hasOriginalFileUrl()) {
9 published.add(file.getOriginalFileUrl());
10 }
11}

Building CDN URLs

Uploadcare CDN can process images by adding directives like this:

https://1gkkwxp4lp.ucarecd.net/85b5644f-e692-4855-9db0-8c5a83096e25/-/resize/640x640/image.jpg

Here’s a Java example to automate CDN URL creation:

1File file = client.getFile("85b5644f-e692-4855-9db0-8c5a83096e25");
2CdnPathBuilder builder = file.cdnPath()
3 .resizeWidth(200)
4 .cropCenter(200, 200)
5 .grayscale();
6URI url = Urls.cdn(builder);

Upload files with FileUploader and UrlUploader

Check out two options to upload files with the Java Uploadcare library Uploader interface.

Upload with UrlUploader:

1Uploader uploader = new UrlUploader(client, "https://example.com/image.jpg");
2File file = uploader.upload();
3System.out.println(file.getFileId());
4System.out.println(file.getOriginalFileUrl());

Upload with FileUploader by providing java.io.File:

1java.io.File localFile = new java.io.File("/path/to/file.jpg");
2Uploader uploader = new FileUploader(client, localFile);
3File file = uploader.upload();
4System.out.println(file.getFileId());
5System.out.println(file.getOriginalFileUrl());

More upload options

Upload from bytes or stream

1// From byte array
2byte[] bytes = Files.readAllBytes(Paths.get("/path/to/file.jpg"));
3Uploader bytesUploader = new FileUploader(client, bytes, "file.jpg");
4File bytesFile = bytesUploader.upload();
5
6// From InputStream
7InputStream stream = new FileInputStream("/path/to/file.jpg");
8Uploader streamUploader = new FileUploader(client, stream, "file.jpg");
9File streamFile = streamUploader.upload();

Signed uploads

1Uploader uploader = new FileUploader(client, localFile);
2uploader.signedUpload("YOUR_SIGNATURE", "YOUR_EXPIRE_TIMESTAMP");
3File file = uploader.upload();

Generate the signature and expire timestamp on your backend. See signed uploads documentation for implementation details.

Batch file operations

1// Store multiple files
2List<String> fileIds = Arrays.asList("UUID_1", "UUID_2", "UUID_3");
3client.saveFiles(fileIds);
4
5// Delete multiple files
6client.deleteFiles(fileIds);

Manage project data and files

With Client, you get a few utility methods to retrieve and handle the project-specific data, like fetching a file by a file id or listing all uploaded files for the project. Note, ids are Uploadcare UUIDs associated with files whenever they get uploaded.

Here’s a short example of navigating your files and retrieving 10 file names uploaded to your project after today’s midnight and matching some arbitrary pattern:

1// Print 10 file names uploaded after today's midnight and matching regexp
2client.getFiles().asList()
3 .stream()
4 .map(f -> new Pair<>(f.getOriginalFilename(), f.getUploadDate()))
5 .filter(p -> p.getValue().after(Date.from(todayMidnight))
6 && p.getKey().matches(FILENAME_STARTING_WITH_LETTERS_AND_HAVING_EXTENSION))
7 .limit(10)
8 .forEach(System.out::println);

Automatic File Storing

When Automatic File Storing in storage settings is on (default), all newly uploaded files will be stored permanently. If it’s switched off, all incoming files will be deleted in 24 hours to prevent your storage cluttering.

With Automatic File Storing. turned off, you can still store files selectively via API. In Java, these per-file controls are provided by both com.uploadcare.api.Client#saveFile taking a UUID as a parameter and com.uploadcare.api.File#save.

Transfer files to Amazon S3

You can connect an Amazon S3 Bucket to your Uploadcare account as custom storage for specific cases. See S3 storage docs on how to configure it.

With the Java library, you can copy files to S3 storage using the copy methods on Client. You can do the same via remote_copy REST API.

1// Copy to Uploadcare local storage
2CopyFile localCopy = client.copyFileLocalStorage("YOUR_FILE_UUID", true);
3
4// Copy to custom remote storage
5CopyFile remoteCopy = client.copyFileRemoteStorage("YOUR_FILE_UUID", "YOUR_STORAGE_NAME", true, null);

More examples

Refer the GitHub and check out more java examples.

Full documentation

Read the full documentation on GitHub.