1

I want to know how can I show the download percentage with this code. If you know an easier method than what I use please post a message about it.

Here it is my code :

public class DownloadURL {

private static String URL;
private static String outputFile;

public static void setURL(String url){
    URL = url;
}

public static void setOutputFileName(String name){
    outputFile=name;
}

public static void resetURL(){
    URL = null;
}

public static void resetOutputFileName(){
    outputFile=null;
}

public static void downloadFromURL(){
    URL website;
    if (URL != null || outputFile != null) {
        try {
            website = new URL(URL);
            ReadableByteChannel rbc = Channels.newChannel(website.openStream());
            FileOutputStream fos = new FileOutputStream(outputFile);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
        } catch (Exception e) {
            Main.consolePrinter("Error in args or invalid page", new Type());
            resetOutputFileName();
            resetURL();
        }
    }
}

}

EDIT : NEW CODE

Download the files and print the percentage in the console :D

Thanks guys for helping me !

I'm french btw

public class DownloadURL {

private static String URL;
private static String outputFile;

public static void setURL(String url){
    URL = url;
}

public static void setOutputFileName(String name){
    outputFile=name;
}

public static void resetURL(){
    URL = null;
}

public static void resetOutputFileName(){
    outputFile=null;
}

public static void downloadFromURL(){
    if (URL != null || outputFile != null) {
        try {
            URL url = new URL(URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int filesize = connection.getContentLength();
            float totalDataRead = 0;
            try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream())) {
                FileOutputStream fos = new FileOutputStream(outputFile);
                try (java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024)) {
                    byte[] data = new byte[1024];
                    int i;
                    while ((i = in.read(data, 0, 1024)) >= 0) {
                        totalDataRead = totalDataRead + i;
                        bout.write(data, 0, i);
                        float Percent = (totalDataRead * 100) / filesize;
                        Main.consolePrinter("Downloading Percent : " + Percent + "%", new Type());
                    }
                }
            }
        } catch (Exception e) {
            javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, e.getMessage(), "Error",
                    javax.swing.JOptionPane.DEFAULT_OPTION);
        }
    }else{
        Main.consolePrinter("Empty args !", new Type());
    }
}

}

Vincent
  • 23
  • 5
  • http://stackoverflow.com/questions/21954581/using-swing-gui-to-make-a-progress-bar-show-up-while-downloading-a-file – sasikumar Mar 05 '16 at 12:36
  • @sasikumar: we have no indication from the original poster that their program is a Swing GUI, and so your link may not be relevant. (although it has a **great** answer!) – Hovercraft Full Of Eels Mar 05 '16 at 12:37
  • @HovercraftFullOfEels depends on what aspect the OP is having difficulty - calculating the percentage, or displaying it. The Q just says 'show', which could be interpreted either way. The linked Q has useful info on how to calculate the percentage. – NickJ Mar 05 '16 at 12:41
  • Going to display the percentage in the console so I need to know how can I calculate it :) – Vincent Mar 05 '16 at 12:43
  • I found how to do thanks guys :D – Vincent Mar 05 '16 at 12:50
  • @Vincent you can post your solution as an answer to your own question, it's better for the future readability of this post ;-) – Jérémie B Mar 05 '16 at 13:02

0 Answers0