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 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

Java字符串处理全解析(String、StringBuilder与StringBuffer)

《Java字符串处理全解析(String、StringBuilder与StringBuffer)》:本文主要介绍Java字符串处理全解析(String、StringBuilder与StringBu... 目录Java字符串处理全解析:String、StringBuilder与StringBuffer一、St

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测