0

I am using the following code to upload a file to server using a PUT request. This works properly. But I want to add a JProgressBar to this this code, how do you suggest I do that? I am not sure of which classes to use to achieve the progressbar I need.

RequestEntity re = new RequestEntity() {
    @Override
    public long getContentLength() {
        // TODO Auto-generated method stub
        return file.length();
    }

    @Override
    public String getContentType() {
        // TODO Auto-generated method stub
        return "application/octet-stream";
    }

    @Override
    public boolean isRepeatable() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void writeRequest(OutputStream outputStream) throws IOException {
        // TODO Auto-generated method stub
        InputStream in = new FileInputStream(file);
        try {
            int l;
            byte[] buffer = new byte[1024];
            while ((l = in.read(buffer)) != -1) {
                outputStream.write(buffer, 0, l);
            }
        } finally {
            in.close();
        }
    }
};
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user1386101
  • 1,945
  • 1
  • 14
  • 21
  • You haven't shown any GUI code whatsoever, do you have any? Or do you expect others to write it? – roippi Jul 19 '13 at 22:52
  • Take a look at [this](http://stackoverflow.com/questions/12185924/show-progress-during-ftp-file-upload-in-a-java-applet/12186144#12186144) and [this](http://stackoverflow.com/questions/16272842/multithreading-in-java-i-want-both-ui-and-code-to-execute-in-parallel/16272933#16272933) for examples – MadProgrammer Jul 20 '13 at 00:10
  • I am sory roippi about that, but there is too much of code put, thats the reason I did not put it up. – user1386101 Jul 20 '13 at 03:06
  • For better help sooner, post an [SSCCE](http://sscce.org/). Tip: Add @roippi (or whoever - the `@` is important) to *notify* them of a new comment. – Andrew Thompson Jul 20 '13 at 06:49
  • oh hi I just got notified of a new comment – roippi Jul 20 '13 at 06:50

2 Answers2

1

Every time a byte is read, add one to a counter. Provide a method to return the number of bytes total and the number of bytes read. In the GUI code, set the maximum value of the progress bar to the number of bytes, and start a timer that periodically calls progress.setValue with the current number of bytes read.

tbodt
  • 16,609
  • 6
  • 58
  • 83
1

Just put a javax.swing.ProgressMonitorInputStream into the input stack.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user207421
  • 305,947
  • 44
  • 307
  • 483