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

相关文章

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

Spring Boot统一异常拦截实践指南(最新推荐)

《SpringBoot统一异常拦截实践指南(最新推荐)》本文介绍了SpringBoot中统一异常处理的重要性及实现方案,包括使用`@ControllerAdvice`和`@ExceptionHand... 目录Spring Boot统一异常拦截实践指南一、为什么需要统一异常处理二、核心实现方案1. 基础组件

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时