java實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳
所謂文件的斷點(diǎn)續(xù)傳,就是一個(gè)線程傳輸文件,另一個(gè)線程控制傳輸標(biāo)識(shí),以達(dá)到暫停文件效果、恢復(fù)文件上傳的效果。
本demo使用最基本的線程之間的通信來實(shí)現(xiàn)一個(gè)簡(jiǎn)單的斷點(diǎn)續(xù)傳。
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);//創(chuàng)建線程池,可自行定義 ? ? ? ? Task task = new Task(true, bis, bos, fileName); ? ? ? ? pool.execute(task); ? ? ? ? Scanner scanner = new Scanner(System.in);//TODO 輸入文字并敲回車,開啟一個(gè)新線程控制文件的上傳、暫停(在上傳和暫停間來回切換) ? ? ? ? while (scanner.hasNextLine()) { ? ? ? ? ? ? String s = scanner.nextLine(); ? ? ? ? ? ? pool.execute(task); ? ? ? ? } ? ? ? ? } ? } ? @Data @AllArgsConstructor @NoArgsConstructor class Task implements Runnable { ? ? ? private volatile Boolean flag = true;//控制文件繼續(xù)、暫停上傳的標(biāo)識(shí),true為繼續(xù)上傳,false暫停上傳 ? ? private BufferedInputStream bis; ? ? private BufferedOutputStream bos; ? ? private String flagName;//監(jiān)聽器,一般設(shè)置為用戶編號(hào)+文件名,防止不同用戶上傳相同為文件。但本demo只以文件名作為監(jiān)聽器 ? ? ? @Override ? ? public void run() { ? ? ? ? String threadName = Thread.currentThread().getName(); ? ? ? ? //TODO 對(duì)線程的后綴名進(jìn)行判斷,如果是不是第一個(gè)線程,則判斷為控制線程 ? ? ? ? if (!threadName.endsWith("1")) { ? ? ? ? ? ? this.flag = !flag; ? ? ? ? } ? ? ? ? synchronized (flagName) { ? ? ? ? ? ? if (!threadName.endsWith("1")) { ? ? ? ? ? ? ? ? ? System.out.println(threadName + "控制線程開始運(yùn)行"); ? ? ? ? ? ? ? ? ? if (flag) { ? ? ? ? ? ? ? ? ? ? flagName.notifyAll(); ? ? ? ? ? ? ? ? ? ? System.out.println("繼續(xù)傳輸文件"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? System.out.println(threadName + "傳輸線程開始運(yùn)行"); ? ? ? ? ? ? ? ? ? ? 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 + "控制線程運(yùn)行完畢"); ? ? ? ? } ? ? } }
在文件通道阻塞時(shí),打開文件屬性,觀察文件大小是否停止增加。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03SpringBoot自定義內(nèi)容協(xié)商的實(shí)現(xiàn)
在Spring Boot中,內(nèi)容協(xié)商是一種機(jī)制,它允許服務(wù)器根據(jù)客戶端的請(qǐng)求選擇返回不同的表示形式,本文就來詳細(xì)的介紹一下SpringBoot自定義內(nèi)容協(xié)商的實(shí)現(xiàn),感興趣的可以了解一下2024-09-09mybatis對(duì)象List<String> List<Integer>屬性映射方式
這篇文章主要介紹了mybatis對(duì)象List<String> List<Integer>屬性映射方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java并發(fā)編程回環(huán)屏障CyclicBarrier
這篇文章主要介紹了Java并發(fā)編程回環(huán)屏障CyclicBarrier,文章繼續(xù)上文所介紹的Java并發(fā)編程同步器CountDownLatch展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-04-04Spring Boot的filter(過濾器)簡(jiǎn)單使用實(shí)例詳解
過濾器(Filter)的注冊(cè)方法和 Servlet 一樣,有兩種方式:代碼注冊(cè)或者注解注冊(cè),下面通過實(shí)例給大家介紹Spring Boot的filter(過濾器)簡(jiǎn)單使用,一起看看吧2017-04-04