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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依