Skip to content

Instantly share code, notes, and snippets.

@ashikuzzaman-ar
Created November 10, 2017 21:04
Show Gist options
  • Save ashikuzzaman-ar/59952f64ccf361e71b1c21bcea7d813c to your computer and use it in GitHub Desktop.
Save ashikuzzaman-ar/59952f64ccf361e71b1c21bcea7d813c to your computer and use it in GitHub Desktop.
Downloading a file from remote server
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
final String from = "http://www.mirrorservice.org/sites/ftp.gimp.org/pub/gimp/v2.8/gimp-2.8.10.tar.bz2";
final String to = "/home/ashik/gimp-2.8.10.tar.bz2";
InputStream inputStream = new URL(from).openStream();
System.out.println("Starting!");
final File file = new File(to);
if (file.exists()) {
System.out.println("File deleted : " + file.delete());
}
try {
new Thread(new Runnable() {
public void run() {
int timeCounter = 0;
while (true) {
try {
System.out.println("Time : " + (timeCounter++));
if (file.exists()) {
long size = Files.size(Paths.get(to)) / 1024;
System.out.println("File Size : " + size + " KB");
System.out.println("Average speed : " + (size / timeCounter) + " KBPS");
}
Thread.sleep(998L);
System.out.println("\n\n\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
Files.copy(inputStream, Paths.get(to));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Finished!");
System.out.println("Total Downloaded : " + Files.size(Paths.get(to)));
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment