2013-04-04 69 views
1

当我上传超过5MB的文件时,进度条在达到4%时自动重置,并且每10秒从0开始。但是,对于5MB以下的文件,进度条可以正常工作并达到100%进度条重置自己?

这是因为maxBufferSize?服务器的最大发布大小是512MB,其他的都是无限的,所以问题必须来自我的代码,但我不知道在哪里。

这里是我的代码

@Override 
protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 

    //update Dialog box progress 
    mProgressDialog.setProgress(progress[0]); 
     if (nofti_appended) 
     {  
     //update Notification progress 
      mBuilder.setProgress(100, progress[0], false); 
     } 
} 


protected String doInBackground(String... urls) {  
      String upLoadServerUri = upApi.uploadUrl; 
      String fileName = this.file_path; 
      HttpURLConnection connection = null; 
      DataOutputStream outputStream = null; 
      String lineEnd = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "*****"; 
      int bytesRead, bytesAvailable, bufferSize; 
      byte[] buffer; 
      int maxBufferSize = 1*1024*1024; 
      File sourceFile = new File(fileName); 
      int sentBytes = 0; 
      long fileSize = sourceFile.length(); 

      try 
       { 
        FileInputStream fileInputStream = new FileInputStream(new File(fileName)); 

        URL url = new URL(upLoadServerUri); 
        connection = (HttpURLConnection) url.openConnection(); 

        // Allow Inputs & Outputs 
        connection.setDoInput(true); 
        connection.setDoOutput(true); 
        connection.setUseCaches(false); 
        connection.setChunkedStreamingMode(1024); 
        // Enable POST method 
        connection.setRequestMethod("POST"); 

        connection.setRequestProperty("Connection", "Keep-Alive"); 
        connection.setRequestProperty("Content-Type",  "multipart/form-data;boundary="+boundary); 

        outputStream = new DataOutputStream( connection.getOutputStream()); 
        outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
        outputStream.writeBytes("Content-Disposition: form-data; name=\"file[]\";filename=\""+ fileName + "\"" + lineEnd); 
        outputStream.writeBytes(lineEnd); 

        bytesAvailable = fileInputStream.available(); 
        Log.v("Size",bytesAvailable+""); 

        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        buffer = new byte[bufferSize]; 


        // Read file 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

        while (bytesRead > 0) 
        { 

         if(isCancelled()){ 

          break; 
         } 

         sentBytes += bytesRead; 
         publishProgress((int)(sentBytes * 100/fileSize)); 

         outputStream.write(buffer, 0, bufferSize); 

         bytesAvailable = fileInputStream.available(); 

         bufferSize = Math.min(bytesAvailable,  maxBufferSize); 
         bytesRead = fileInputStream.read(buffer, 0,  bufferSize); 
        } 


        if(isCancelled()){ 

         return "faild"; 
        } 

        outputStream.writeBytes(lineEnd); 
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

        Scanner s; 
        s = new Scanner(connection.getInputStream()); 
        s.useDelimiter("\\Z"); 
        final String response = s.next(); 

        // Responses from the server (code and message) 
        int serverResponseCode  = connection.getResponseCode(); 

        fileInputStream.close(); 
        outputStream.flush(); 
        outputStream.close(); 

        if(serverResponseCode == 200) 
        { 
         return response; 
        } 


       } catch (MalformedURLException ex) { 

       } catch (final Exception e) { 

       } 

      return "faild"; 
     } 

任何想法?

+1

您需要修剪这个删除所有这一切无关与实际上传过程中的额外的代码。内容类型和其他所有内容都不需要,并且使您的实际问题难以回答。请[编辑您的问题]删除除了与您在此询问的问题相关的代码之外的所有内容,以便我们尝试帮助您找到解决方案。谢谢。 :-) – 2013-04-04 22:47:52

+0

现在呢?我的问题很难理解!所有我需要知道为什么进度条重置为0时,其大文件达到4% – 2013-04-05 00:49:41

+0

publishProgress((int)(sentBytes * 100/fileSize));如果您的进度条重置,那么该行必须设置为0。你为什么乘着sentBytes * 100? – 2013-04-05 01:03:53

回答

1

我想你实际上有两个问题,你的进步计算:

首先,你的逻辑是不完全正确。它应该是(见下段为理由):

(sentBytes/fileSize) * 100 

第二个问题,不过,是你使用int值,而是试图做一个浮点运算。 (百分比是0.01 - 1.00,然后它乘以100将它变成1% - 100%。)您需要以浮点形式进行计算,然后将最终值转换为整数。

尝试像

publishProgress((int)(((sentBytes * 1.0)/fileSize) * 100))); 

这越来越成为一个很大的()双,虽然。这将是更好宣布独立floatdouble变量,并用它来代替:

percentDone = (sentBytes * 1.0)/fileSize * 100; 
publishProgress((int)percentDone); 
+0

它的工作非常感谢你兄弟,上帝保佑你的一天 – 2013-04-05 01:22:39

+0

它工作非常感谢你,上帝保佑你的一天 – 2013-04-05 01:23:35