2

I want my jProgressBar to update its value during HTTP File Upload. I'm new to Java and I'm not sure I'm doing the right things, here's my code:

private static final String Boundary = "--7d021a37605f0";

public void upload(URL url, File f) throws Exception
{
    HttpURLConnection theUrlConnection = (HttpURLConnection) url.openConnection();
    theUrlConnection.setDoOutput(true);
    theUrlConnection.setDoInput(true);
    theUrlConnection.setUseCaches(false);
    theUrlConnection.setChunkedStreamingMode(1024);

    theUrlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
            + Boundary);

    DataOutputStream httpOut = new DataOutputStream(theUrlConnection.getOutputStream());


        String str = "--" + Boundary + "\r\n"
                   + "Content-Disposition: form-data;name=\"file1\"; filename=\"" + f.getName() + "\"\r\n"
                   + "Content-Type: image/png\r\n"
                   + "\r\n";

        httpOut.write(str.getBytes());

        FileInputStream uploadFileReader = new FileInputStream(f);
        int numBytesToRead = 1024;
        int availableBytesToRead;
        jProgressBar1.setMaximum(uploadFileReader.available());
        while ((availableBytesToRead = uploadFileReader.available()) > 0)
        {
            jProgressBar1.setValue(jProgressBar1.getMaximum() - availableBytesToRead);
            byte[] bufferBytesRead;
            bufferBytesRead = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead]
                    : new byte[availableBytesToRead];
            uploadFileReader.read(bufferBytesRead);
            httpOut.write(bufferBytesRead);
            httpOut.flush();
        }
        httpOut.write(("--" + Boundary + "--\r\n").getBytes());

    httpOut.flush();
    httpOut.close();

    // read & parse the response
    InputStream is = theUrlConnection.getInputStream();
    StringBuilder response = new StringBuilder();
    byte[] respBuffer = new byte[4096];
    while (is.read(respBuffer) >= 0)
    {
        response.append(new String(respBuffer).trim());
    }
    is.close();
    System.out.println(response.toString());
}

Is this line jProgressBar1.setValue(jProgressBar1.getMaximum() - availableBytesToRead); correct ?

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
user1376701
  • 35
  • 1
  • 5

2 Answers2

6

Something like one in every 30 questions tagged java here has the same solution as yours. You are doing all your work inside an event handler, which means that it's happening on the Event Dispatch Thread -- and blocks all further GUI updates until it's over. You must use a SwingWorker and delegate your work to it.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Ok, thanks for answering, but how can I fetch the upload progress from the worker please ? – user1376701 May 06 '12 at 14:05
  • What have you tried? Do you have code that fails to compile, or at runtime, do you have problems with sharing the reference to `jProgressBar1` to the other thread...? – Marko Topolnik May 06 '12 at 14:08
3

I second @Marko Topolnic's suggestion of using a SwingWorker , Have a look at these useful links to get you further on Howto ,

  1. How to Use Progress Bars
  2. Concurrency in Swing
  3. Worker Threads and SwingWorker

and an example by @trashgod.

Community
  • 1
  • 1
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • Thanks a lot to you and @Marko Topolnic ! Your example was very useful, I put my function in an independent worker and that worked ! – user1376701 May 06 '12 at 19:31