亲测有效,可自己试用
package com.ant.ai.xiaoheng.dataDownload.download; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Date; /** * 多线程下载 * * */ public class DownloadURLFile { public String down(String href) throws InterruptedException { long begin_time = new Date().getTime(); URL url = null; try { url = new URL(href); } catch (MalformedURLException e1) { e1.printStackTrace(); } URLConnection conn = null; try { conn = url.openConnection(); } catch (IOException e1) { e1.printStackTrace(); } String fileName = url.getFile(); fileName = fileName.substring(fileName.lastIndexOf("/")); System.out.println("开始下载>>>"); int fileSize = conn.getContentLength(); System.out.println("文件总共大小:" + fileSize + "字节"); // 设置分块大小 int blockSize = 1024 * 1024; // 文件分块的数量 int blockNum = fileSize / blockSize; if ((fileSize % blockSize) != 0) { blockNum += 1; }