public class DownloadServer {

    private int threadCount = ;
private static String fileUrl = "https://dldir1.qq.com/qqtv/mac/TencentVideo_V2.2.1.42253.dmg";
// private static String fileUrl = "http://statics.garmentnet.cn/file/file_photo/show/news/5c3c055c5793d567739439.jpg"; private static ExecutorService executorService = new ThreadPoolExecutor(, , , TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new Download(), new ThreadPoolExecutor.DiscardPolicy()); private static ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(, new Download()); private int getFileInfo() {
int count = ;
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
count = urlConnection.getContentLength();
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}
return count;
} private void download(int fileSize) throws IOException {
File file = new File("d.dmg");
if (file.exists()) {
file.delete();
file.createNewFile();
}
int perSize = fileSize / threadCount;
int start;
int end;
for (int i = ; i < threadCount; i++) {
start = i * perSize;
if (i == threadCount - ) {
end = fileSize - ;
} else {
end = (i + ) * perSize - ;
} executorService.execute(new Download(fileUrl, file, start, end));
}
} public static void main(String[] args) {
DownloadServer downloadServer = new DownloadServer();
int size = downloadServer.getFileInfo();
scheduledExecutorService.scheduleAtFixedRate(new DownloadCount(size), , , TimeUnit.MILLISECONDS);
try {
downloadServer.download(size);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Download implements Runnable, ThreadFactory {

    private String fileUrl;
private File file;
private int start;
private int end;
public static AtomicInteger downloadFileSize = new AtomicInteger(0);
private static AtomicInteger threadNum = new AtomicInteger(0); public Download(){ } public Download(String fileUrl, File file, int start, int end) {
this.file = file;
this.start = start;
this.end = end;
this.fileUrl = fileUrl;
} @Override
public void run() {
try {
URL url = new URL(fileUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Range", "bytes=" + start + "-" + end); InputStream inputStream = httpURLConnection.getInputStream();
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
randomAccessFile.seek(start);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
randomAccessFile.write(bytes, 0, len);
downloadFileSize.addAndGet(len);
// System.out.println(Thread.currentThread().getName() + "-" + downloadFileSize.addAndGet(len));
}
randomAccessFile.close(); } catch (IOException e) {
e.printStackTrace();
}
} @Override
public Thread newThread(Runnable r) {
return new Thread(r, "Run-" + threadNum.incrementAndGet());
}
}
public class DownloadCount implements Runnable {

    private int fileSize;
public DownloadCount(int fileSize) {
this.fileSize = fileSize;
} @Override
public void run() {
if(fileSize != Download.downloadFileSize.get()) {
System.out.println(Download.downloadFileSize);
}
}
}
 
05-11 18:19