package cn.itcast.net.download;import java.io.File;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;public class MulThreadDownload { /** * @param args */ public static void main(String[] args) { String path = "http://net.itcast.cn/QQWubiSetup.exe"; try { new MulThreadDownload().download(path, 3); } catch (Exception e) { e.printStackTrace(); } } /** * 从路径中获取文件名称 * @param path 下载路径 * @return */ public static String getFilename(String path){ return path.substring(path.lastIndexOf('/')+1); } /** * 下载文件 * @param path 下载路径 * @param threadsize 线程数 */ public void download(String path, int threadsize) throws Exception{ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); int filelength = conn.getContentLength();//获取要下载的文件的长度 String filename = getFilename(path);//从路径中获取文件名称 File saveFile = new File(filename); RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd"); accessFile.setLength(filelength);//设置本地文件的长度和下载文件相同 accessFile.close(); //计算每条线程下载的数据长度 int block = filelength%threadsize==0? filelength/threadsize : filelength/threadsize+1; for(int threadid=0 ; threadid < threadsize ; threadid++){ new DownloadThread(url, saveFile, block, threadid).start(); } } private final class DownloadThread extends Thread{ private URL url; private File saveFile; private int block;//每条线程下载的数据长度 private int threadid;//线程id public DownloadThread(URL url, File saveFile, int block, int threadid) { this.url = url; this.saveFile = saveFile; this.block = block; this.threadid = threadid; } @Override public void run() { //计算开始位置公式:线程id*每条线程下载的数据长度= ? //计算结束位置公式:(线程id +1)*每条线程下载的数据长度-1 =? int startposition = threadid * block; int endposition = (threadid + 1 ) * block - 1; try { RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd"); accessFile.seek(startposition);//设置从什么位置开始写入数据 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); conn.setRequestProperty("Range", "bytes="+ startposition+ "-"+ endposition); InputStream inStream = conn.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len=inStream.read(buffer)) != -1 ){ accessFile.write(buffer, 0, len); } inStream.close(); accessFile.close(); System.out.println("线程id:"+ threadid+ "下载完成"); } catch (Exception e) { e.printStackTrace(); } } }}
详细解决方案
Android通过HTTP协议实现多线程上载
热度:55 发布时间:2016-05-01 11:37:21.0
相关解决方案
- struts HTTP Status 500解决办法
- httpclient老是提示java.lang.NoSuchMethodError: org.apache.http.protocol.BasicHttpCon
- 启动tomcat后,http://localhost:8080无法打开的有关问题
- 相干tomcat HTTP Status 500的疑问,麻烦大家进来看看
- Web Service 错误:服务器未能识别 HTTP 头 SOAPAction 的值: http://tempuri.org/
- HTTP Status 404 - /jspdemo/Register.jsp,该怎么解决
- 'http://reg.163.com/login.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/,该如何处理
- org.apache.http.client.HttpClient与org.apache.commons.httpclient.HttpClient的差别
- HTTP 404有关问题
- myeclipse<%@ taglib prefix="c" uri="http://java.sun.com/jst1/core_rt"%>解决思路
- ,http 200状态码
- tomcat新手有关问题 HTTP Status 404 - /manager/
- java http 分段上载
- soap调用webservice,报错:Root element of a SOAP message must be: 'http://schemas.xmls,该怎么处理
- HTTP Status 404如何解决
- org.apache.tomcat.util.http.Parameters processParameters,该怎么解决
- 新手,HTTP Status 404 有关问题
- java http 断点上传,该如何处理
- JSP疑问<%@ taglib prefix="ht" uri="http://www.ht.com.cn/jsp/taglibs"%>该怎么处理
- weservice调用Server returned HTTP response code: 500 for URL解决方法
- HTTP Status 404 - /webproject2/servlet/login解决办法
- 报错:HTTP Status 405 - HTTP method GET is not supported by this URL解决思路
- http://newbbs.csdn.net/ 现时和老版的bbs用的还不是同一个库
- http://newbbs.csdn.net/ 现下和老版的bbs用的还不是同一个库
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网