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方式推送進度),涉及到后台計算進度問題,特此記錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM