Here you can find the source of downloadFile(String fileURL, String savePath)
Parameter | Description |
---|---|
fileURL | HTTP URL of the file to be downloaded |
savePath | path of the directory to save the file |
Parameter | Description |
---|---|
IOException | an exception |
public static String downloadFile(String fileURL, String savePath) throws IOException
//package com.java2s; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class Main { /**// ww w .j av a2s . c om * Downloads a file from a URL * * @param fileURL HTTP URL of the file to be downloaded * @param savePath path of the directory to save the file * @throws IOException * @author www.codejava.net */ public static String downloadFile(String fileURL, String savePath) throws IOException { System.err.printf("Downloading %s to %s\n", fileURL, savePath); URL url = new URL(fileURL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); // always check HTTP response code first if (responseCode == HttpURLConnection.HTTP_OK) { String fileName = ""; String disposition = httpConn.getHeaderField("Content-Disposition"); String contentType = httpConn.getContentType(); int contentLength = httpConn.getContentLength(); if (disposition != null) { // extracts file name from header field int index = disposition.indexOf("filename="); if (index > 0) { fileName = disposition.substring(index + 10, disposition.length() - 1); } } else { // extracts file name from URL fileName = new File(httpConn.getURL().getPath()).getName(); } // System.out.println("Content-Type = " + contentType); // System.out.println("Content-Disposition = " + disposition); // System.out.println("Content-Length = " + contentLength); // System.out.println("fileName = " + fileName); // opens input stream from the HTTP connection InputStream inputStream = httpConn.getInputStream(); String saveFilePath = savePath; if (new File(savePath).isDirectory()) saveFilePath = savePath + File.separator + fileName; String realPath; if (new File(saveFilePath).isFile()) { System.err.printf("Use cached %s instead.\n", fileName); realPath = saveFilePath; } else { saveFilePath += ".downloading"; // opens an output stream to save into file FileOutputStream outputStream = new FileOutputStream(saveFilePath); int bytesRead; byte[] buffer = new byte[4096]; long start = System.currentTimeMillis(); int progress_size = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); long duration = (System.currentTimeMillis() - start) / 1000; duration = Math.max(duration, 1); progress_size += bytesRead; int speed = (int) (progress_size / (1024 * duration)); float ratio = progress_size / (float) contentLength; float percent = ratio * 100; int eta = (int) (duration / ratio * (1 - ratio)); int minutes = eta / 60; int seconds = eta % 60; System.err.printf("\r%.2f%%, %d MB, %d KB/s, ETA %d min %d s", percent, progress_size / (1024 * 1024), speed, minutes, seconds); } System.err.println(); outputStream.close(); realPath = saveFilePath.substring(0, saveFilePath.length() - ".downloading".length()); if (!new File(saveFilePath).renameTo(new File(realPath))) throw new IOException("Failed to move file"); } inputStream.close(); httpConn.disconnect(); return realPath; } else { httpConn.disconnect(); throw new IOException("No file to download. Server replied HTTP code: " + responseCode); } } }