多线程并发下载同一文件DEMO

DownloadFile.java

import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class DownloadFile { private File file; private String url; private int startPos; private int endPos; private int totalSize; private String fileName; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getStartPos() { return startPos; } public void setStartPos(int startPos) { this.startPos = startPos; } public int getEndPos() { return endPos; } public void setEndPos(int endPos) { this.endPos = endPos; } public int getTotalSize() { return totalSize; } public void setTotalSize(int totalSize) { this.totalSize = totalSize; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public int getURLTotalSize() throws Exception{ int total = 0; if(this.url != null && !"".equals(this.url)){ URL url = new URL(this.url); HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); return httpConn.getContentLength(); } return total; } public InputStream getInputStream(){ InputStream is = null; try{ if(this.url != null && !"".equals(this.url)){ URL url = new URL(this.url); HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); httpConn.setRequestProperty("Connection", "Keep-Alive"); //保持一直连接 httpConn.setConnectTimeout(60 * 1000 * 5); //连接超时5分钟 httpConn.setRequestMethod("POST"); //以GET方式连接 httpConn.setAllowUserInteraction(true); if(this.startPos != 0 || this.endPos != 0){ httpConn.setRequestProperty("Range", "bytes=" + getStartPos() + "-" + getEndPos()); } return new BufferedInputStream(httpConn.getInputStream(),1024*8); } }catch(Exception ex){ ex.printStackTrace(); } return is; } public static void main(String[] args) throws Exception { DownloadFile downloadFile = new DownloadFile(); downloadFile.setUrl("http://localhost:8080/navigater/admin/123.txt"); downloadFile.setStartPos(5); downloadFile.setEndPos(880); InputStream is = downloadFile.getInputStream(); System.out.println("is------>" +is.available()); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = null; while(null != (str = br.readLine())){ System.out.println(str); } } }
Test.java

import java.io.BufferedInputStream; import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test { public static void main(String[] args) throws Exception { /* InputStream file = new FileInputStream("c:" + File.separator + "123.txt"); byte[] by = new byte[5]; System.out.println(file.markSupported()); // file.mark(1); // file.reset(); file.skip(5); int length = file.read(by,0,5); System.out.println(length); System.out.println(new String(by));*/ Test test = new Test(); test.saveFile(); } public void saveFile() throws Exception{ File file = new File("c:" + File.separator + "456.exe"); if(file.exists())file.delete(); // RandomAccessFile randomFile = new RandomAccessFile(file,"rwd"); URL url = new URL("http://localhost:8080/navigater/admin/MyEclipse_6.5.0GA_E3.3.2_Installer_A.exe"); HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); int totalSize = httpConn.getContentLength(); System.out.println("totalSize---------->" + totalSize); int threadNum = (int)Math.ceil(totalSize/(1024*800f)); System.out.println("threadNum---------->" + threadNum); CountDownLatch threadCount = new CountDownLatch (threadNum); ExecutorService service = Executors.newFixedThreadPool(4); for(int i=0; i<threadNum; i++){ RandomAccessFile randomFile = new RandomAccessFile(file,"rwd"); DownloadFile downloadFile = new DownloadFile(); downloadFile.setUrl("http://localhost:8080/navigater/admin/MyEclipse_6.5.0GA_E3.3.2_Installer_A.exe"); downloadFile.setStartPos(i*1024*800); int ends = (i+1)*1024*800-1; if(ends<totalSize){ downloadFile.setEndPos(ends); }else{ downloadFile.setEndPos(totalSize-1); } downloadFile.setTotalSize(totalSize); randomFile.seek(downloadFile.getStartPos()); service.execute(new MyThread(randomFile,downloadFile, threadCount)); } threadCount.await(); // randomFile.close(); service.shutdown(); System.out.println("全部完成"); } class MyThread implements Runnable{ private RandomAccessFile randomFile; private DownloadFile downloadFile; private CountDownLatch threadCount; public MyThread() { } public MyThread(RandomAccessFile randomFile, DownloadFile downloadFile, CountDownLatch threadCount) { this.randomFile = randomFile; this.downloadFile = downloadFile; this.threadCount = threadCount; } @Override public void run() { try{ InputStream is = this.downloadFile.getInputStream(); byte[] by = new byte[1024*40]; int length = 0; // ByteArrayOutputStream baos = new ByteArrayOutputStream(); while(-1 != (length = is.read(by, 0, by.length))){ // this.randomFile.seek(this.downloadFile.getStartPos()); this.randomFile.write(by,0,length); // baos.write(by,0,length); } // System.out.println(this.downloadFile.getStartPos()+ "~~~~~~~~~~~~~~~~~" + this.downloadFile.getEndPos()+ "~~~~~~~~~~~~~~~~~" + new String(baos.toByteArray())); is.close(); threadCount.countDown(); }catch(Exception ex){ ex.printStackTrace(); } } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值