springboot文件上传 流的方式 后台计算上传进度

//代码

public static void main(String[] args) throws Exception {
  String path = "f:/svn/t_dictionary.txt";
  File file = new File(path);//源文件

  Long fsize = file.length();//获取文件大小
  FileInputStream in = new FileInputStream(file);

  //目标文件
  FileOutputStream out = new FileOutputStream("f:/svn/temp/t_dictionary.txt");
  byte[] buffer = new byte[1024];
  int bz = buffer.length;
  int readNumber = 0;
  long progress = 0;//进度数值 12%显示的是取整 12
  int time = 0;//循环的次数
  while((readNumber = in.read(buffer)) != -1){
    time++;
    progress = time*bz*100/fsize;
    out.write(buffer);
    System.out.println(time+" ======> "+ progress);
  }


}

 说明:要做一个文件上传并且还带有进度条(websocket方式推送进度),涉及到后台计算进度问题,特此记录

原文地址:https://www.cnblogs.com/xuchao0506/p/12843043.html