显示正在进行的上传进度对话框

时间:2015-06-02 07:45:37

标签: android file-upload upload progressdialog

我上传文件并希望显示进度对话框以查看上传进度。我已经包含了一个错误的水平对话框,因为它在1秒内达到100%然后等待(因为它仍在上传)。

我使用以下功能上传:

 private void doFileUpload(String selectedPath){
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    String responseFromServer = "";
    String urlString = "https://xxxxxxxxxx.com";
    try
    {
        //------------------ CLIENT REQUEST
        File f = new File(selectedPath);
        FileInputStream fileInputStream = new FileInputStream(f);

        //calculate size of file
        wholeFilesize = f.length(); //returns the length in Bytes

        // open a URL connection to the Servlet
        URL url = new URL(urlString);
        // Open a HTTP connection to the URL
        conn = (HttpURLConnection) url.openConnection();
        // Allow Inputs
        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=\"upfile\";filename=\"" + selectedPath + "\"" + lineEnd);

        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // read file and write it into form...
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
            totalReadFilesize+=bytesRead;
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            int progress = (int)((totalReadFilesize*100)/(wholeFilesize));
            //Log.i("Progress: ", (int)((totalReadFilesize*100)/(wholeFilesize))+"");
            dialog.setProgress(progress);
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        }
        //publishProgress(100);
        Log.i("Size: ", wholeFilesize+"");
        Log.i("Bytes read: ", totalReadFilesize+"");

        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        Log.e("Debug","File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();
    }
    catch (MalformedURLException ex)
    {
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe)
    {
        Log.e("Debug", "error: " + ioe.getMessage(), ioe);
    }
    //------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream ( conn.getInputStream() );
        String str;

        while (( str = inStream.readLine()) != null)
        {
            Log.e("Debug","Server Response "+str);
            JSONObject jsonObj = new JSONObject(str);
            //String status=(String) jsonObj.get("status");
            Log.i("TEST", (String) jsonObj.get("message"));
            uploadResultMessage = (String) jsonObj.get("message");
            Log.i("TEST", uploadResultMessage);

        }
        inStream.close();

    }
    catch (IOException ioex){
        Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

有人知道,我应该如何修改代码以查看正在上传的文件的真实进度?我觉得很奇怪,因为我在this线程的doInBackground()中做了与用户相同的事情。

0 个答案:

没有答案