Java - Display % of upload done
- by tr-raziel
I have a java applet for uploading files to server.
I want to display the % of data sent but when I use ObjectOutputStream.write() it just writes to the buffer, does not wait until the data has actually been sent. How can I achieve this.
Perhaps I need to use thread synchronization or something. Any clues would be most helpful.
This is the code I'm using right now:
            try{
        for(File file : ficheiros){
            FileInputStream stream = new FileInputStream (file);
            int  bytesRead1 = 0;;
            int off1 = 0; 
            int len1 = 100000;
            if(file.length() < 100000) len1 = new Long(file.length()).intValue();
            byte[] bytes1     = new byte[len1];    
            while (off1 < file.length()) {
                bytes1 = new byte[len1];
                if((file.length() - off1) < len1){
                       len1 = (new Long(file.length()).intValue() - off1);
                       bytes1 = new byte[len1];
                }
                if((bytesRead1 = stream.read(bytes1)) != -1){
                    //I want this to block until all data has been sent
                    outputToServlet.write(bytes1, 0, bytesRead1 ); 
                    System.out.println("off1: " + off1);
                    off1 = off1 + len1;    
                    outputToServlet.flush();
                }
                sent += len1;
                if(sent>totalLength)
                    sent = (int)totalLength;
                updateFeedback(sent,totalLength,false);//calls method to display %
            }
            updateFeedback(-1,-1,true);
        }
        }catch(Exception e){
            e.printStackTrace();
        }
Thanks