tus-java-client
tus is a protocol based on HTTP for resumable file uploads. Resumable
means that an upload can be interrupted at any moment and can be resumed without
re-uploading the previous data again. An interruption may happen willingly, if
the user wants to pause, or by accident in case of a network issue or server
outage.
tus-java-client is a library for uploading files using the tus protocol to any remote server supporting it.
This library is also compatible with the Android platform and can be used without any modifications using the API. The tus-android-client provides additional classes which can be used in addition the Java library.
Usage
TusClient client = new TusClient();
client.setUploadCreationURL(new URL("https://tusd.tusdemo.net/files"));
client.enableResuming(new TusURLMemoryStore());
File file = new File("./cute_kitten.png");
final TusUpload upload = new TusUpload(file);
System.out.println("Starting upload...");
TusExecutor executor = new TusExecutor() {
@Override
protected void makeAttempt() throws ProtocolException, IOException {
TusUploader uploader = client.resumeOrCreateUpload(upload);
uploader.setChunkSize(1024);
do {
long totalBytes = upload.getSize();
long bytesUploaded = uploader.getOffset();
double progress = (double) bytesUploaded / totalBytes * 100;
System.out.printf("Upload at %06.2f%%.\n", progress);
} while(uploader.uploadChunk() > -1);
uploader.finish();
System.out.println("Upload finished.");
System.out.format("Upload available at: %s", uploader.getUploadURL().toString());
}
};
executor.makeAttempts();
Installation
The JARs can be downloaded manually from our Maven Central Repo
Gradle:
implementation 'io.tus.java.client:tus-java-client:0.5.0'
Maven:
<dependency>
<groupId>io.tus.java.client</groupId>
<artifactId>tus-java-client</artifactId>
<version>0.5.0</version>
</dependency>
Documentation
The documentation of the latest version (main branch of git repository) can be found online at tus.github.io/tus-java-client/javadoc/.
FAQ
Can I use my own custom SSLSocketFactory?
Yes, you can! Create a subclass of TusClient
and override the prepareConnection
method to attach your SSLSocketFactory
. After this use your custom TusClient
subclass as you would normally use it. Here is an example:
@Override
public void prepareConnection(@NotNull HttpURLConnection connection) {
super.prepareConnection(connection);
if(connection instanceof HttpsURLConnection) {
HttpsURLConnection secureConnection = (HttpsURLConnection) connection;
secureConnection.setSSLSocketFactory(mySSLSocketFactory);
}
}
Can I use a proxy that will be used for uploading files?
Yes, just add a proxy to the TusClient as shown below (1 line added to the above usage):
TusClient client = new TusClient();
Proxy myProxy = new Proxy(...);
client.setProxy(myProxy);
License
MIT