SpringBoot可恢复文件上传tus-java-client库的使用

2024-01-20 07:18

本文主要是介绍SpringBoot可恢复文件上传tus-java-client库的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、说明

tus是一种基于HTTP的可恢复文件上传协议。可恢复意味着上传可以随时中断,并且可以恢复,而无需再次重新上传以前的数据。如果用户想要暂停,中断可能会自动发生,或者在网络问题或服务器中断的情况下意外发生。

tus-java-client是一个库,用于使用tus协议将文件上载到支持它的任何远程服务器。

该库还与Android平台兼容,使用API时无需任何修改即可使用。tus android客户端提供了额外的类,这些类可以在Java库之外使用。
源码地址: https://github.com/tus/tus-java-client

2、Maven依赖

<dependency><groupId>io.tus.java.client</groupId><artifactId>tus-java-client</artifactId><version>0.4.3</version>
</dependency>

3、示例代码

// Create a new TusClient instance
TusClient client = new TusClient();// Configure tus HTTP endpoint. This URL will be used for creating new uploads
// using the Creation extension
client.setUploadCreationURL(new URL("https://tusd.tusdemo.net/files"));// Enable resumable uploads by storing the upload URL in memory
client.enableResuming(new TusURLMemoryStore());// Open a file using which we will then create a TusUpload. If you do not have
// a File object, you can manually construct a TusUpload using an InputStream.
// See the documentation for more information.
File file = new File("./cute_kitten.png");
final TusUpload upload = new TusUpload(file);System.out.println("Starting upload...");// We wrap our uploading code in the TusExecutor class which will automatically catch
// exceptions and issue retries with small delays between them and take fully
// advantage of tus' resumability to offer more reliability.
// This step is optional but highly recommended.
TusExecutor executor = new TusExecutor() {@Overrideprotected void makeAttempt() throws ProtocolException, IOException {// 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.uploader.setChunkSize(1024);// Upload the file as long as data is available. Once the// file has been fully uploaded the method will return -1do {// 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 upuploader.finish();System.out.println("Upload finished.");System.out.format("Upload available at: %s", uploader.getUploadURL().toString());}
};
executor.makeAttempts();

这篇关于SpringBoot可恢复文件上传tus-java-client库的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/625188

相关文章

Java实现按字节长度截取字符串

《Java实现按字节长度截取字符串》在Java中,由于字符串可能包含多字节字符,直接按字节长度截取可能会导致乱码或截取不准确的问题,下面我们就来看看几种按字节长度截取字符串的方法吧... 目录方法一:使用String的getBytes方法方法二:指定字符编码处理方法三:更精确的字符编码处理使用示例注意事项方

pandas DataFrame keys的使用小结

《pandasDataFramekeys的使用小结》pandas.DataFrame.keys()方法返回DataFrame的列名,类似于字典的键,本文主要介绍了pandasDataFrameke... 目录Pandas2.2 DataFrameIndexing, iterationpandas.DataF

使用Python和PaddleOCR实现图文识别的代码和步骤

《使用Python和PaddleOCR实现图文识别的代码和步骤》在当今数字化时代,图文识别技术的应用越来越广泛,如文档数字化、信息提取等,PaddleOCR是百度开源的一款强大的OCR工具包,它集成了... 目录一、引言二、环境准备2.1 安装 python2.2 安装 PaddlePaddle2.3 安装

嵌入式Linux之使用设备树驱动GPIO的实现方式

《嵌入式Linux之使用设备树驱动GPIO的实现方式》:本文主要介绍嵌入式Linux之使用设备树驱动GPIO的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、设备树配置1.1 添加 pinctrl 节点1.2 添加 LED 设备节点二、编写驱动程序2.1

Spring三级缓存解决循环依赖的解析过程

《Spring三级缓存解决循环依赖的解析过程》:本文主要介绍Spring三级缓存解决循环依赖的解析过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、循环依赖场景二、三级缓存定义三、解决流程(以ServiceA和ServiceB为例)四、关键机制详解五、设计约

spring IOC的理解之原理和实现过程

《springIOC的理解之原理和实现过程》:本文主要介绍springIOC的理解之原理和实现过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、IoC 核心概念二、核心原理1. 容器架构2. 核心组件3. 工作流程三、关键实现机制1. Bean生命周期2.

解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException: org.junit.Test问题

《解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException:org.junit.Test问题》:本文主要介绍解决tomcat启动时报Junit相... 目录tomcat启动时报Junit相关错误Java.lang.ClassNotFoundException

Gradle下如何搭建SpringCloud分布式环境

《Gradle下如何搭建SpringCloud分布式环境》:本文主要介绍Gradle下如何搭建SpringCloud分布式环境问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Gradle下搭建SpringCloud分布式环境1.idea配置好gradle2.创建一个空的gr

JVM垃圾回收机制之GC解读

《JVM垃圾回收机制之GC解读》:本文主要介绍JVM垃圾回收机制之GC,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、死亡对象的判断算法1.1 引用计数算法1.2 可达性分析算法二、垃圾回收算法2.1 标记-清除算法2.2 复制算法2.3 标记-整理算法2.4

springboot集成Lucene的详细指南

《springboot集成Lucene的详细指南》这篇文章主要为大家详细介绍了springboot集成Lucene的详细指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起... 目录添加依赖创建配置类创建实体类创建索引服务类创建搜索服务类创建控制器类使用示例以下是 Spring