java - 如何计算上传任务的百分比

标签 java android upload progressdialog

我在UploadAsync 类中有一个doFileUpload 方法,它扩展了AsyncTask。 我想知道如何根据以下代码计算上传任务的百分比,以使用 publishProgress(""+values)

更新 ProgressDialog
 private void doFileUpload(){
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null; 
        String exsistingFileName = "/sdcard/six.3gp";
        // Is this the place are you doing something wrong.
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;
        String urlString = "http://192.168.1.5/upload.php";
        try
        {
            Log.e("MediaPlayer","Inside second Method");
            FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
            dos = new DataOutputStream( conn.getOutputStream() );
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
            dos.writeBytes(lineEnd);
            Log.e("MediaPlayer","Headers are written");
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0)
            {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) 
                tv.append(inputLine);
            // close streams
            Log.e("MediaPlayer","File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
        }
        catch (MalformedURLException ex)
        {
            Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
        }
        catch (IOException ioe)
        {
            Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
        }

        //------------------ read the SERVER RESPONSE
        try {
            inStream = new DataInputStream ( conn.getInputStream() );
            String str;            
            while (( str = inStream.readLine()) != null)
            {
                Log.e("MediaPlayer","Server Response"+str);
            }
            /*while((str = inStream.readLine()) !=null ){

            }*/
            inStream.close();
        }
        catch (IOException ioex){
            Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
        }
    }

谢谢

最佳答案

这就是我在申请中所做的

 long total = 0;
 int lenghtOfFile=connection.getContentLength();

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress(""+(int)((total*100)/lenghtOfFile));
                // writing data to file
                output.write(data, 0, count);
                if (isCancelled()) break;
            }

你可以在这里看到它:The Official documentation

关于java - 如何计算上传任务的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31529826/

相关文章:

java - AWS IoT SDK - 构建 AWSIotData 和 AWSIotClient 时出现异常

java - 是否可以一次将所有 JPanes 设置为相同的背景颜色?

codeigniter - 如何使用 codeigniter 将两个图像上传到两个字段

upload - 如何批量上传到 s3?

java - C3po 创建过多连接

java - 未使用字段 'nameOfField' 的值 - Java Swing

android - WebView 中的不可见缩放控件

android - 如何设置放置在 ImageView 上的 TextView 的背景不透明度

Android更改 fragment 时隐藏键盘

javascript - WebKitFormBoundary 包含在直接上传到 s3 的文件有效负载中