java实现文件的断点续传
作者:hailier_9502 时间:2023-11-23 09:11:00
所谓文件的断点续传,就是一个线程传输文件,另一个线程控制传输标识,以达到暂停文件效果、恢复文件上传的效果。
本demo使用最基本的线程之间的通信来实现一个简单的断点续传。
package com.test;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("d:\\12bb.eif");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("d:\\ddxc\\bqb.eif");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
String fileName = file.getName();
ExecutorService pool = Executors.newFixedThreadPool(30);//创建线程池,可自行定义
Task task = new Task(true, bis, bos, fileName);
pool.execute(task);
Scanner scanner = new Scanner(System.in);//TODO 输入文字并敲回车,开启一个新线程控制文件的上传、暂停(在上传和暂停间来回切换)
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
pool.execute(task);
}
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Task implements Runnable {
private volatile Boolean flag = true;//控制文件继续、暂停上传的标识,true为继续上传,false暂停上传
private BufferedInputStream bis;
private BufferedOutputStream bos;
private String flagName;// * ,一般设置为用户编号+文件名,防止不同用户上传相同为文件。但本demo只以文件名作为 *
@Override
public void run() {
String threadName = Thread.currentThread().getName();
//TODO 对线程的后缀名进行判断,如果是不是第一个线程,则判断为控制线程
if (!threadName.endsWith("1")) {
this.flag = !flag;
}
synchronized (flagName) {
if (!threadName.endsWith("1")) {
System.out.println(threadName + "控制线程开始运行");
if (flag) {
flagName.notifyAll();
System.out.println("继续传输文件");
}
} else {
try {
System.out.println(threadName + "传输线程开始运行");
int len = 0;
byte[] cbuf = new byte[1024];
while ((len = bis.read(cbuf, 0, cbuf.length)) != -1) {
if (!flag) {
System.out.println("文件通道阻塞中");
flagName.wait();
}
bos.write(cbuf, 0, len);
bos.flush();
Thread.sleep(10);
}
System.out.println("文件传输完毕");
bis.close();
bos.close();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(threadName + "控制线程运行完毕");
}
}
}
在文件通道阻塞时,打开文件属性,观察文件大小是否停止增加。
来源:https://blog.csdn.net/qq_34951638/article/details/123780024
标签:java,断点续传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
jbuilder2006连接sqlserver2000的方法
2022-08-21 14:20:09
Android Studio真机无线连接USB设备调试运行详解流程
2023-12-13 00:37:37
![](https://img.aspxhome.com/file/2023/7/88117_0s.png)
Unity3D实现人物转向与移动
2022-10-28 08:42:13
Android 自定义闪屏页广告倒计时view效果
2021-10-13 16:26:39
![](https://img.aspxhome.com/file/2023/2/94992_0s.png)
Kotlin协程Dispatchers原理示例详解
2022-09-26 00:09:45
Unity 按钮事件封装操作(EventTriggerListener)
2022-07-08 10:07:08
![](https://img.aspxhome.com/file/2023/7/113687_0s.jpg)
Android实现连连看游戏
2021-08-22 09:51:17
idea项目的左侧目录没了如何设置
2022-03-21 15:45:43
![](https://img.aspxhome.com/file/2023/1/86801_0s.jpg)
java中对字符串每个字符统计的方法
2023-09-23 13:53:16
为什么阿里要慎重使用ArrayList中的subList方法
2023-12-07 19:03:34
![](https://img.aspxhome.com/file/2023/0/82380_0s.jpg)
双重检查锁定模式Java中的陷阱案例
2023-11-13 22:11:02
![](https://img.aspxhome.com/file/2023/5/59575_0s.png)
JavaMail与Spring整合过程解析
2022-07-06 23:53:54
Java多维数组和Arrays类方法总结详解
2022-04-27 22:30:18
java数据结构与算法之中缀表达式转为后缀表达式的方法
2023-11-22 12:51:35
Mybatis MapperScannerConfigurer自动扫描Mapper接口生成代理注入到Spring的方法
2023-04-17 11:57:25
Android Force Close 出现的异常原因分析及解决方法
2021-09-07 18:26:47
Java游戏俄罗斯方块的实现实例
2022-11-26 12:35:49
SpringBoot请求发送与信息响应匹配实现方法介绍
2021-07-08 22:05:27
![](https://img.aspxhome.com/file/2023/4/91064_0s.png)
window下安装和配置maven环境
2021-06-13 14:32:16
![](https://img.aspxhome.com/file/2023/4/63664_0s.png)
SpringBoot配置log4j输出日志的案例讲解
2023-07-30 18:54:50