package org.bee.webBee.download;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.bee.webBee.HttpClient.HttpResponse;
import org.bee.webBee.linker.Request;
import org.bee.webBee.processor.Task;

import java.io.*;

/**
 * data 2017-05-11   01:35
 * E-mail   sis.nonacosa@gmail.com
 *
 * @author sis.nonacosa
 */
public class FileDownloader {

    private Request request;

    private Task task ;

    public FileDownloader(Request request,Task task){
        this.request = request;
        this.task = task;
    }

    public void downFile(String path , String fileName){
        CloseableHttpResponse response = HttpResponse.getInstance(request,task).getResponse();
        File xml = new File(path+fileName+".mp4");
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(xml);
            InputStream inputStream = response.getEntity().getContent();
            byte buff[] = new byte[4096];
            int counts;
//            int count = 0;
            System.out.println("正在下载 : " + fileName );
            while ((counts = inputStream.read(buff)) != -1) {
                outputStream.write(buff, 0, counts);
            }
            outputStream.flush();
            outputStream.close();
            System.out.println(fileName + "下载完成!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}

@@ -410,17 +410,16 @@
 ask task
- 
 ;%0A%0A    p
@@ -455,16 +455,17 @@
 request,
+ 
 Task tas
@@ -466,16 +466,17 @@
 sk task)
+ 
 %7B%0A      
@@ -570,17 +570,16 @@
 ing path
- 
 , String
@@ -588,16 +588,17 @@
 ileName)
+ 
 %7B%0A      
@@ -665,16 +665,17 @@
 request,
+ 
 task).ge
@@ -699,51 +699,18 @@
     
-File xml = new File(path+fileName+%22.mp4%22);%0A
+try %7B%0A    
     
@@ -750,40 +750,16 @@
  = n
-ull;%0A        try %7B%0A            o
+ew FileO
 utpu
@@ -761,35 +761,33 @@
 OutputStream
- = 
+(
 new File
 OutputStream
@@ -778,24 +778,34 @@
 File
-OutputStream(xml
+(path + fileName + %22.mp4%22)
 );%0A 
@@ -888,16 +888,144 @@
         
+Double streamLength = (double) response.getEntity().getContentLength();%0A            Double readStreamLength = 0.00;%0A            
 byte buf
@@ -1050,34 +1050,8 @@
 6%5D;%0A
-            int counts;%0A//
     
@@ -1067,20 +1067,17 @@
 nt count
- = 0
+s
 ;%0A      
@@ -1121,17 +1121,16 @@
 fileName
- 
 );%0A     
@@ -1239,16 +1239,161 @@
 ounts);%0A
+                readStreamLength += counts;%0A                System.out.print(%22%5Cr %E5%B7%B2%E7%BB%8F%E4%B8%8B%E8%BD%BD%25%22 + Math.floor((readStreamLength / streamLength) * 100) );%0A
         
@@ -1398,16 +1398,17 @@
       %7D%0A
+%0A
         
@@ -1495,18 +1495,16 @@
 ut.print
-ln
 (fileNam
@@ -1673,17 +1673,16 @@
     %7D%0A%0A%0A
-%0A
     %7D%0A%7D%0A

complete downFile