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();
- 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;
- }
- } catch (Exception exception) {
- exception.printStackTrace();
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- } catch (IOException ioe) {
- }
- }
- }
Add Comment
Please, Sign In to add comment