0

저는 Apache와 Async를 사용하여 FTP로 파일을 업로드하고 있습니다. 문제는 큰 파일 (예 : 200MB)에서 진행률 표시 줄이 진행 상태를 다시 시작하지만 업로드가 계속 진행 중입니다 (컴퓨터에서 FTP를 통해 확인).진행 표시 줄이 다시 시작됩니다.

1->2->3->4->5->6->7->8-> -8 -> -7 .... to 8 and starts again 

비동기 배경 코드 : 오래

con = new FTPClient(); 
con.setConnectTimeout(300000); 
con.setDataTimeout(300000); 
// con.setKeepAlive(true); 

con.connect("host IP",21);     
if (con.login("user", "password")) 
{ 
    con.enterLocalPassiveMode(); // important! 
    con.setFileType(FTP.BINARY_FILE_TYPE); 

    boolean directoryExists = con.changeWorkingDirectory(Email); 
    File CloudVersionSysForRestore = new File(getFilesDir()+File.separator+"Cloud Version.itb"); 

    try { 
     CloudVersionSysForRestore.createNewFile(); 
     OutputStream OutstreamCloud = new FileOutputStream(new File(CloudVersionSysForRestore.getPath()));         
     con.retrieveFile("/"+Email+"/Cloud Version.itb", OutstreamCloud); 

     OutstreamCloud.close(); 

     if(x!=2){ 
      BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(getFilesDir()+File.separator+"Cloud Version.itb"))); 
      String read; 
      StringBuilder builder = new StringBuilder(""); 

      while((read = bufferedReader.readLine()) != null) { 
       Log.w("READ", read); 
       builder.append(read); 
      } 
      bufferedReader.close(); 

      long getintfromstring = Long.parseLong(builder.toString());   
      if(getintfromstring<Version){ 
       Log.w("Version", String.valueOf(Version)); 
       try{ 
/************************************ UPLOAD + Progress ****************************/ 
        long fileSize = DBToUploadFromInternalStorage.length(); 
        int sentBytes = 0; 
        InputStream inputStream = new FileInputStream(DBToUploadFromInternalStorage);   
        System.out.println("Start uploading second file"); 
        OutputStream outputStream = con.storeFileStream("PilotLogbookDB.itb"); 
        byte[] bytesIn = new byte[4 * 1024]; 
        int read1 = 0;       
        while ((read1 = inputStream.read(bytesIn)) !=-1) { 
         outputStream.write(bytesIn, 0, read1); 
         sentBytes+=read1; 
         final int progress = (int) ((sentBytes * 100)/fileSize); 

         runOnUiThread(new Runnable() { 
          public void run() { 
           pd.setProgress(progress); 
           Log.w("Progress", String.valueOf(progress)); 
          } 
         }); 
        }         
        outputStream.close(); 
        inputStream.close(); 

/************************************ UPLOAD + Progress ****************************/ 
       } 
       catch(Exception e){ 
        e.printStackTrace(); 
        x=2; 
       } 

       boolean completed = con.completePendingCommand(); 

는이처럼

이것은 내가 로그 캣에서 볼 무엇인가?

/************************************ UPLOAD + Progress ****************************/ 
               long fileSize = DBToUploadFromInternalStorage.length(); 
               long sentBytes = 0; 
               InputStream inputStream = new FileInputStream(DBToUploadFromInternalStorage);  

                System.out.println("Start uploading second file"); 
                OutputStream outputStream = con.storeFileStream("PilotLogbookDB.itb"); 
                byte[] bytesIn = new byte[4 * 1024]; 
                int read1 = 0;       
                while ((read1 = inputStream.read(bytesIn)) !=-1) { 

                 outputStream.write(bytesIn, 0, read1); 
                 sentBytes+=read1; 
                 final long progress = (long) ((sentBytes * 100)/fileSize); 



                 runOnUiThread(new Runnable() { 
                  public void run() { 

                   pd.setProgress(Long.bitCount(progress)); 
                   Log.w("Progress", String.valueOf(progress)); 




                  } 
                 }); 
                } 


                outputStream.close(); 
                inputStream.close(); 








               /************************************ UPLOAD + Progress ****************************/ 

답변

0

당신은 당신이 요소 (100)에 의해 사용할 수있는 실제 바이트 수를 줄일 수 (100)에 의해 진행 카운터를 곱 final int progress = /*...*/ 라인에 있지만, 진행 카운터 (sentBytes)로 int을 사용하고, 당신은 가야 로 변경하여 높은 가능한 값 : 당신이 훨씬 더 큰 파일 (> 2GB의)이있는 경우,

final int progress = (int) ((((double) sentBytes)/fileSize) * 100); 

다른 방법으로, 당신은 long를 사용하는 것이 좋습니다.

+0

질문에 긴 버전을 추가했습니다 ... 괜찮습니까? – Iosif

+0

아니요, 필자가 (Long과 관련하여) 쓴 내용이 잘못되었습니다. pd.setProgress ((int) progress); – Iosif

+0

'Long.bitCount (progress)'는 내가 뭘 원하는지와 다릅니다. 여러분의 크기가 이제'long '이므로 문제를 고쳤으니'(int) ((sentBytes * 100)/fileSize)'는 0에서 100 사이의 적절한 정수를 제공합니다. 'final int progress = (int) ((sentBytes * 100)/fileSize)'. – dst