tus-java-client

repository·main·Indexed 42 days ago

https://github.com/tus/tus-java-client

A Java library for performing resumable file uploads using the tus protocol, compatible with standard Java environments and the Android platform. Version 0.5.1 supports chunked uploading, automatic retries via TusExecutor, custom SSLSocketFactory configurations, and proxy support.

Tokens
1.4K
Snippets
4
Records
4
Agent score
4%

What's inside tus-java-client

  1. Perform a resumable file upload

    main

    To upload a file using the tus protocol, follow these steps:

    1. Initialize TusClient: Set the upload creation URL using setUploadCreationURL(URL).
    2. Enable Resuming: Use enableResuming(TusURLStore) (e.g., TusURLMemoryStore) to store upload URLs so uploads can resume after interruptions.
    3. Create a TusUpload: Wrap your File in a TusUpload object.
    4. Use TusExecutor (Recommended): Wrap your upload logic in a TusExecutor to automatically handle retries and exceptions.
    5. Execute Upload: Inside makeAttempt(), call client.resumeOrCreateUpload(upload) to get a TusUploader.
    6. Chunked Uploading: Set a chunk size with uploader.setChunkSize(long) and loop through uploader.uploadChunk() until it returns -1.
    7. Cleanup: Call uploader.finish() to close the connection.
    // Create a new TusClient instance
    TusClient client = new TusClient();
    
    // Configure tus HTTP endpoint
    client.setUploadCreationURL(new URL("https://tusd.tusdemo.net/files"));
    
    // Enable resumable uploads by storing the upload URL in memory
    client.enableResuming(new TusURLMemoryStore());
    
    File file = new File("./cute_kitten.png");
    final TusUpload upload = new TusUpload(file);
    
    System.out.println("Starting upload...");
    
    // Use TusExecutor for automatic retries
    TusExecutor executor = new TusExecutor() {
        @Override
        protected void makeAttempt() throws ProtocolException, IOException {
            // Resume or create a new upload
            TusUploader uploader = client.resumeOrCreateUpload(upload);
            
            // Upload the file in chunks of 1KB sizes
            uploader.setChunkSize(1024);
    
            // Upload until the file is fully uploaded (returns -1)
            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);
    
            // Cleanup
            uploader.finish();
    
            System.out.println("Upload finished.");
            System.out.format("Upload available at: %s", uploader.getUploadURL().toString());
        }
    };
    executor.makeAttempts();
  2. Install the tus-java-client

    main

    You can install the library using Gradle or Maven. For manual installation, download the JARs from Maven Central.

    // Gradle
    implementation 'io.tus.java.client:tus-java-client:0.5.1'
    <!-- Maven -->
    <dependency>
      <groupId>io.tus.java.client</groupId>
      <artifactId>tus-java-client</artifactId>
      <version>0.5.1</version>
    </dependency>
  3. Use a custom SSLSocketFactory

    main

    To use a custom SSLSocketFactory, create a subclass of TusClient and override the prepareConnection method to attach your factory to the HttpsURLConnection.

    @Override
    public void prepareConnection(@NotNull HttpURLConnection connection) {
        super.prepareConnection(connection);
        
        if(connection instanceof HttpsURLConnection) {
            HttpsURLConnection secureConnection = (HttpsURLConnection) connection;
            secureConnection.setSSLSocketFactory(mySSLSocketFactory);
        }
    }