apache - Java ApacheIO File download progress on console using copyInputStreamToFile -


i'm trying find way use

copyinputstreamtofile(inputstream source, file destination) 

to make small progress bar in console file size. there way this?

the short answer can't, @ source code of method, tried track execution path , goes method @ ioutils class:

 public static long copylarge(final inputstream input, final outputstream output, final byte[] buffer)                 throws ioexception {             long count = 0;             int n = 0;             while (eof != (n = input.read(buffer))) {                 output.write(buffer, 0, n);                 count += n;             }             return count;         } 

so, functionality encapsulated api.

the long answer can implement downloading method yourself, using relative parts of ioutils , fileutils libraries , add functionality print percentage of downloaded file in console:

this working kick-off example:

package apache.utils.custom; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import org.apache.commons.io.fileutils; import org.apache.commons.io.ioutils; public class downloader {      private static final int eof = -1;     private static final int default_buffer_size = 1024 * 4;       public static void copyinputstreamtofilenew(final inputstream source, final file destination, int filesize) throws ioexception {         try {              final fileoutputstream output = fileutils.openoutputstream(destination);             try {                  final byte[] buffer = new byte[default_buffer_size];                 long count = 0;                 int n = 0;                 while (eof != (n = source.read(buffer))) {                     output.write(buffer, 0, n);                     count += n;                     system.out.println("completed " + count * 100/filesize + "%");                 }                  output.close(); // don't swallow close exception if copy completes             } {                 ioutils.closequietly(output);             }          } {             ioutils.closequietly(source);         } } 

you should provide expected file size method can calculate using code:

        url url = new url(urlstring);         urlconnection urlconnection = url.openconnection();         urlconnection.connect();         int file_size = urlconnection.getcontentlength(); 

of course better idea encapsulate whole functionality in single method.

hope you.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -