使用org.apache.commons.io包中的FileUtils工具类实现:
public static void downloadHttpUrl(String url, String dir, String fileName) throws IOException {
URL urlPath = new URL(url);
File file = new File(dir);
if (!file.exists()) {
file.mkdirs();
}
FileUtils.copyURLToFile(urlPath, new File(dir + fileName));
}
获取待下载文件的大小:
public static long getHttpFileLenth(String path) throws IOException {
long length = 0;
URL url = new URL(path);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
length = urlConnection.getContentLengthLong();
urlConnection.disconnect();
return length;
}