Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ProgressBar with HTTP Upload don't update

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 ?

like image 566
user1376701 Avatar asked Feb 19 '26 00:02

user1376701


2 Answers

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.

like image 138
Marko Topolnik Avatar answered Feb 21 '26 13:02

Marko Topolnik


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.

like image 39
COD3BOY Avatar answered Feb 21 '26 12:02

COD3BOY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!