首页 > 编程语言 >tus java client 使用以及问题说明

tus java client 使用以及问题说明

时间:2023-09-19 13:11:09浏览次数:31  
标签:java tus upload client uploader new

代码来自官方参考,支持在使用的时候发现了一些问题记录下

参考代码

  • App.java
package org.example;
 
import io.tus.java.client.*;
 
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
 
public class App {
    public static void main(String[] args) throws IOException, io.tus.java.client.ProtocolException {
        TusClient client = new TusClient();
        client.setUploadCreationURL(new URL("http://localhost:1080/files/"));
        client.enableResuming(new TusURLMemoryStore());
        File file = new File("src/main/resources/my.mp4");
        final TusUpload upload = new TusUpload(file);
        Map<String,String> medata  = new HashMap<>();
        medata.put("filename", "my.mp4");
        medata.put("filetype", "video/mp4");
        System.out.println("Starting upload...");
        upload.setMetadata(medata);
        TusExecutor executor = new TusExecutor() {
            @Override
            protected void makeAttempt() throws IOException, io.tus.java.client.ProtocolException {
                // First try to resume an upload. If that's not possible we will create a new
                // upload and get a TusUploader in return. This class is responsible for opening
                // a connection to the remote server and doing the uploading.
                TusUploader uploader = client.resumeOrCreateUpload(upload);
                // Alternatively, if your tus server does not support the Creation extension
                // and you obtained an upload URL from another service, you can instruct
                // tus-java-client to upload to a specific URL. Please note that this is usually
                // _not_ necessary and only if the tus server does not support the Creation
                // extension. The Vimeo API would be an example where this method is needed.
                // TusUploader uploader = client.beginOrResumeUploadFromURL(upload, new URL("https://tus.server.net/files/my_file"));
                // Upload the file in chunks of 1KB sizes.
               //  此处是一个问题,因为我们进行上传的文件比较大,我设置了setChunkSize 为8M,但是tus 会提示offset 409 问题
                uploader.setChunkSize(1024*1024*8);
               // 解决方法,就是同时也设置setRequestPayloadSize 这样可以确保对于大文件上传没有问题
                uploader.setRequestPayloadSize(1024*1024*8);
                // Upload the file as long as data is available. Once the
                // file has been fully uploaded the method will return -1
                do {
                    // Calculate the progress using the total size of the uploading file and
                    // the current offset.
                    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);
                // Allow the HTTP connection to be closed and cleaned up
                uploader.finish();
                System.out.println("Upload finished.");
                System.out.format("Upload available at: %s", uploader.getUploadURL().toString());
            }
        };
        executor.makeAttempts();
    }
}

说明

tus-java-client 维护并不是很频繁,同时对于TusURLStore 设计主要提供了基于内存的,如果我们有特殊场景可以自己扩展下

参考资料

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

标签:java,tus,upload,client,uploader,new
From: https://www.cnblogs.com/rongfengliang/p/17714360.html

相关文章

  • 如何在JavaScript中实现链表
      转载来自:https://www.freecodecamp.org/news/implementing-a-linked-list-in-javascript/  Ifyouarelearningdatastructures,alinkedlistisonedatastructureyoushouldknow.IfyoudonotreallyunderstanditorhowitisimplementedinJavaScript......
  • 无涯教程-JavaScript - ROUND函数
    描述ROUND函数将数字四舍五入为指定的位数。ROUND是Excel舍入函数之一。语法ROUND(number,num_digits)争论Argument描述Required/OptionalnumberThenumberthatyouwanttoround.Requirednum_digitsThenumberofdigitstowhichyouwanttoroundthenum......
  • Java8(jdk1.8u321)下载、安装及配置
    jdk1.8的下载、jdk1.8的安装、配置环境变量。一、jdk1.8下载下载官网地址:https://www.oracle.com/java/technologies/downloads/#java8-windows下载版本:jdk-8u381-windows-x64.exe二、jdk1.8的安装双击jdk1.8安装包进行安装:按照安装步骤,直至完成安装。确认是否完成安装:打......
  • JAVA17/JAVA21继承和抽象类
    day07-JAVAOOP课程目标1.【理解】什么是继承2.【掌握】继承的使用3.【理解】继承中的成员的特点4.【理解】super和this的作用5.【理解】什么是抽象类6.【掌握】抽象的使用继承继承概述​ 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,这个类就是......
  • Java stream 中 peek() 的合理用法
    目录背景最终操作(terminaloperation)peek()vsforEach()peek()的典型用法:协助调试小结参考链接背景这周遇到了一件很有意思的事情。在看项目代码时,发现了这么一段:returnobjects.stream() .peek(object->addInfo(object,someParams)) .collect(Collectors.toL......
  • Glide源码阅读之状态模式[SingleRequest<R>.Status]
    前言前面写完策略模式,接着写状态模式;在开始接触这两个模式的时候我也很疑惑,这两个设计模式很相似,用法也很类似。好一段时间我都没有区分这两者的区别。在使用的时候也不知道怎么选择,后来慢慢的加深理解也就总结出规律了。先看看状态模式的经典结构状态模式介绍《Android源码设计模......
  • 62基于java的高校奖学金管理系统设计与实现(可参考做毕业设计)
    本章节给大家带来一个基于java高校奖学金管理系统设计与实现,可适用于大学奖学金管理系统,学生奖学金管理系统,学校奖学金,校园奖学金申请管理系统;引言以往的高校奖学金管理系统相关信息管理,都是工作人员手工统计。这种方式不但时效性低,而且需要查找和变更的时候很不方便。随着科学......
  • tus-node-server 官方基于node 实现的tus server 1.0 发布
    tus-node-server是基于typescript开发的新的nodejsserver,通过拆分为独立的包(file,s3,gcs,server)包含的特性完全基于typescript开发拆分为了独立的包提供了新的events以及hooks升级了依赖包重新设计了存储file存储添加了过去扩展更好的周边集成(express,fastify。。。)......
  • HttpClient MultipartFormDataContent
    varfileStream=newMemoryStream(fileContent);varparametersList=newMultipartFormDataContent{{newStringContent(folderName),"FolderName"},{newStringContent(fileNam......
  • 万字长文深度解读Java线程池,硬核源码分析
    前言本文将深入分析Java线程池的源码,包括线程池的创建、任务提交、工作线程的执行和线程池的关闭等过程。通过对线程池源码的解析,我们能够更好地理解线程池的原理和机制,为我们在实际开发中合理使用线程池提供指导。文章内容较长,建议找个安静的环境慢慢细读,由于线程池涉及的内容......