java 实现文件下载进度条

计算不同用户的下载进度

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;public class ProgressBarThread implements Runnable {private ArrayList<Integer> proList = new ArrayList<Integer>();private int progress;//当前进度private int totalSize;//下载文法总大小private boolean run = true;private int showProgress;public static HashMap<String, Integer> progressMap = new HashMap<>();//各用户的下载进度public static HashMap<String, Boolean> executeStatusMap = new HashMap<>();//各用户是否下载中private Long fileId;private String token;public ProgressBarThread(int totalSize, Long fileId, String token) {this.totalSize = totalSize;this.fileId = fileId;this.token = token;//创建进度条时,将指定用户的执行状态改为trueexecuteStatusMap.put(token, true);}/*** @param progress 进度*/public void updateProgress(int progress) {synchronized (this.proList) {if (this.run) {this.proList.add(progress);this.proList.notify();}}}public void finish() {this.run = false;//关闭进度条}@Overridepublic void run() {synchronized (this.proList) {try {while (this.run) {if (this.proList.size() == 0) {this.proList.wait();}synchronized (proList) {this.progress += this.proList.remove(0);//更新进度条showProgress = (int) ((new BigDecimal((float) this.progress / this.totalSize).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()) * 100);progressMap.put(token + fileId, showProgress);if (showProgress == 100) {//进度100%时将用户的执行状态改为falseexecuteStatusMap.put(token, false);}System.err.println("当前进度:"+showProgress+"%");}}System.err.println("下载完成");} catch (Exception e) {e.printStackTrace();}}}public static void main(String[] args) {try {File file = new File("C:/Users/mfy/Desktop/蜜雪冰城.pdf");FileInputStream fis = new FileInputStream(file);FileOutputStream fos = new FileOutputStream("D:/nms/img/蜜雪冰城.pdf");ProgressBarThread pbt = new ProgressBarThread((int) file.length(), 1L, "000001");//创建进度条new Thread(pbt).start();//开启线程,刷新进度条byte[] buf = new byte[1024];int size = 0;while ((size = fis.read(buf)) > -1) {//循环读取fos.write(buf, 0, size);pbt.updateProgress(size);//写完一次,更新进度条}pbt.finish();//文件读取完成,关闭进度条fos.flush();fos.close();fis.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

获取获取下载进度,可以通过webSocket推送,也可以前端定时调用

    public HzRestResult getProgress(String fileId, HttpServletRequest request) {String token = request.getHeader("token");Integer progress = ProgressBarThread.progressMap.get(token + fileId);if (progress == null) {return HzRestResult.getFailed("该任务不存在!");}if(progress.equals(100)){//进度100%时清除指定mapProgressBarThread.progressMap.remove(token + fileId);}JSONObject jsonObject = new JSONObject();jsonObject.put("fileId", fileId);jsonObject.put("token", token);jsonObject.put("progress", progress);return HzRestResult.getSuccess(jsonObject);}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部