Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void download(String address, String localFileName, String saveTo) {
- OutputStream out = null;
- URLConnection conn = null;
- InputStream in = null;
- try {
- File f = new File(saveTo + localFileName);
- f.createNewFile();
- URL url = new URL(address);
- out = new BufferedOutputStream(new FileOutputStream(saveTo + localFileName));
- conn = url.openConnection();
- in = conn.getInputStream();
- byte[] buffer = new byte[1024];
- int numRead;
- long numWritten = 0;
- int length = conn.getContentLength();
- long startTime = System.currentTimeMillis();
- while ((numRead = in.read(buffer)) != -1)
- {
- int percentage = (int)(((double)numWritten / (double)length) * 100D);
- //percentage.. need estimated time..
- out.write(buffer, 0, numRead);
- numWritten += numRead;
- long timeElapsed = System.currentTimeMillis() - startTime;
- double avgSpeed = (double) numWritten / timeElapsed;
- double timeLeft = (length - numWritten) / avgSpeed;
- System.out.println("Milliseconds until download finishes: " + timeLeft);
- }
- } catch (Exception exception) {
- exception.printStackTrace();
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- } catch (IOException ioe) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement