How to show the progress of copying Java files in ICEfaces?

I have an ICEfaces web application that should perform a file copy operation and show the progress bar to the user.

Currently, copying is done by calling 'cpio', which cannot push Java code until the operation completes. Although one could use Java to track the number of bytes written versus the number of bytes read to measure copy progress, I think there might be a simpler solution if I code the actual copy operation in Java. I would still use cpio for archiving purposes, but the actual copy would be executed by the Java class.

Most of the help I found in my search was related to progressMonitor, which includes the swing component, and I'm not sure if it can do what I want. All I need is an integer / double stroke number that I can feed to my JSF progress bar component as% of 100.

+4
source share
2 answers

I don't know about ICEFaces, but here's how to copy a file in java and track progress on the command line:

import java.io.*; public class FileCopyProgress { public static void main(String[] args) { System.out.println("copying file"); File filein = new File("test.big"); File fileout = new File("test_out.big"); FileInputStream fin = null; FileOutputStream fout = null; long length = filein.length(); long counter = 0; int r = 0; byte[] b = new byte[1024]; try { fin = new FileInputStream(filein); fout = new FileOutputStream(fileout); while( (r = fin.read(b)) != -1) { counter += r; System.out.println( 1.0 * counter / length ); fout.write(b, 0, r); } } catch(Exception e){ System.out.println("foo"); } } } 

You need to update the progress bar somehow, not System.out.println() . Hope this helps, but maybe I didn't understand your question.

+5
source

Basically, you should use a listener template. In this example, lsitener has a single method for receiving the just-written byte. It can be pre-populated with the total file size to calculate the percentage displayed on the screen.

 ReadableByteChannel source = ...; WritableByteChannel dest = ...; FileCopyListener listener = ...; ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE); while (src.read(buf) != -1) { buf.flip(); int writeCount = dest.write(buf); listener.bytesWritten(writeCount); buf.compact(); } buf.flip(); while (buffer.hasRemaining()) { int writeCount = dest.write(buf); listener.bytesWritten(writeCount); } 
+1
source

All Articles