public class DownloadServer {

    private int threadCount = 4;
    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(4, 4, 180, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10), new Download(), new ThreadPoolExecutor.DiscardPolicy());

    private static ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1, new Download());

    private int getFileInfo() {
        int count = 0;
        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 = 0; i < threadCount; i++) {
            start = i * perSize;
            if (i == threadCount - 1) {
                end = fileSize - 1;
            } else {
                end = (i + 1) * perSize - 1;
            }

            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), 0, 10, 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);
}
}
}
 

 


posted on 2019-03-28 20:38  sidesky  阅读(541)  评论(0编辑  收藏  举报