showing a progress bar during file upload

后端 未结 5 1250
心在旅途
心在旅途 2021-01-21 03:47

I\'ve got an async task that is supposed to show progress during a file upload. Everything is working except that it looks like it finishes the file upload really really fast,

相关标签:
5条回答
  • 2021-01-21 03:55

    You can try using a AsyncTask ...

    create a progress dialog in onPreExecute method and dismiss the dialog in onPostExecute method..

    Keep the upload method in doInBackground()

    example :

    public class ProgressTask extends AsyncTask<String, Void, Boolean> {
    
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            dialog = new ProgressDialog(context);
        }
    
        /** progress dialog to show user that the backup is processing. */
        private ProgressDialog dialog;
    
        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }
    
            @Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }           
        }
    
        protected Boolean doInBackground(final String... args) {
    

    // your upload code

              return true;
           }
        }
    }
    
    0 讨论(0)
  • 2021-01-21 03:55

    HttpURLConnection.setFixedLengthStreamingMode(...) did the trick!

    0 讨论(0)
  • 2021-01-21 03:57

    You can do like:

    try { // open a URL connection to the Servlet
                FileInputStream fileInputStream = new FileInputStream(
                        sourceFile);
                URL url = new URL("http://10.0.2.2:9090/plugins/myplugin/upload");
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true); // Allow Inputs
                conn.setDoOutput(true); // Allow Outputs
                conn.setUseCaches(false); // Don't use a Cached Copy
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                conn.setRequestProperty("Content-Type",
                        "multipart/form-data;boundary=" + boundary);
                conn.setRequestProperty("uploadedfile", filename);
                // conn.setFixedLengthStreamingMode(1024);
                // conn.setChunkedStreamingMode(1);
                dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                        + filename + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                bytesAvailable = fileInputStream.available();
                bufferSize = (int) sourceFile.length()/200;//suppose you want to write file in 200 chunks
                buffer = new byte[bufferSize];
                int sentBytes=0;
                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    // Update progress dialog
                    sentBytes += bufferSize;
                    publishProgress((int)(sentBytes * 100 / bytesAvailable));
                    bytesAvailable = fileInputStream.available();
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }
                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // Responses from the server (code and message)
                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();
                // close streams
                fileInputStream.close();
                dos.flush();
                dos.close();
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    0 讨论(0)
  • 2021-01-21 04:02

    This is how HTTP Post is designed to work so do not expect it to give you progress details.

    You can use one of the several file uploader components available in the market. They internally use flash or silverlight or iframes to show the progress.

    http://dhtmlx.com/docs/products/dhtmlxVault/index.shtml

    http://www.element-it.com/multiple-file-upload/flash-uploader.aspx

    You will find many such others if you google a bit.

    They internally use raw IO instead of http post to handle multiple files and progress notification. Yahoo and Google also uses such techniques for making attachments to mail.

    If you are really feeling adventurous, you can recreate the wheel - i.e. write your own component.

    Edit:

    Please specify if you want to do this in a windows desktop application or a web application.

    0 讨论(0)
  • 2021-01-21 04:07

    You can use Progress Dialog class as follows,

    ProgressDialog progDailog = ProgressDialog.show(this,"Uploading", "Uploading File....",true,true);
    
    new Thread ( new Runnable()
    {
         public void run()
         {
          // your loading code goes here
         }
    }).start();
    
    Handler progressHandler = new Handler() 
    {
         public void handleMessage(Message msg1) 
         {
             progDailog.dismiss();
         }
    }
    
    0 讨论(0)
提交回复
热议问题