断点续传java代码

一、
/*** 1、实现文件下载*/
public class Demo01 {public static void main(String[] args) {String path="http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";try {//创建一个URL对象URL url = new URL(path);//打开连接,获取了url请求的连接对象connHttpURLConnection cnn = (HttpURLConnection) url.openConnection();//设置请求cnn.setConnectTimeout(5000);//请求超时时间cnn.setReadTimeout(5000);//读取超时时间cnn.setRequestMethod("GET");//设置请求提交的方法int responseCode = cnn.getResponseCode();//获取响应码System.out.println("responseCode = " + responseCode);//如果连接ok  200if (responseCode == 200) {//下载?InputStream in = cnn.getInputStream();//获取用于读取网络资源的输入流//下载的流//String fileName = conn.getHeaderField("Content-Disposition");//获取响应的头部文件(包含由文件名)//fileName = fileName.split(";")[1];//fileName = fileName.substring(fileName.indexOf("\"")+1,fileName.lastIndexOf("\""));//System.out.println(fileName);//成功获取文件名File file = new File("QQPCDownload1843.exe");RandomAccessFile raf = new RandomAccessFile(file,"rwd");//可读、可写,有权限//读一组,下载一组byte[]bytes=new byte[1024];int len = 0;while((len=in.read(bytes))!=-1){raf.write(bytes,0,len);}//关闭流释放资源raf.close();in.close();}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
}

二、

/*** 断点续传*  分段下载*/
public class Demo02 {public static void main(String[] args) {int threadCount = 3;//分为3段,创建3个线程对象来下载String path="http://softforspeed.51xiazai.cn/down/BaiduNetdisk_6.9.7.4.exe";try {//创建一个URL对象URL url = new URL(path);//打开连接,获取了url请求的连接对象connHttpURLConnection conn = (HttpURLConnection) url.openConnection();//请求设置//conn.setConnectTimeout(5000);//请求的超时时间//conn.setReadTimeout(5000);//读取超时时间//conn.setRequestMethod("GET");//设置请求提交的方法Get Post//获取响应码int responseCode = conn.getResponseCode();//获取响应码System.out.println("responseCode = " + responseCode);//如果连接ok 200if(responseCode==200){//设计分段下载(如分成3次下载)//要下载的文件大小int contentLength = conn.getContentLength();//每个线程下载的大小int size = contentLength/threadCount;/*每个下载3个线程1:0 1 2    start=i*size end=(i+1)*size-1线程2:3 4 5   start=i*size end=(i+1)*size-1线程3:6 7 8 9 start=i*size end=9(最后一个线程最好直接到长度)*///3.开启线程执行下载for (int i = 0; i < threadCount; i++) {//创建断点续传线程对象(下载一部分)DownLoadThread dt = new DownLoadThread();//设置下载哪一部分(start,end)dt.setPath(path);dt.setStart(i*size);if(i==2){//如果是最后一段数据,就到文件结尾dt.setEnd(contentLength);}else{dt.setEnd((i+1)*size);}//线程对象.start();dt.start();}}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}//0 1 2 3 4 5 6 7 8 9   10个数据//10/3=3}
}
class DownLoadThread extends Thread {private String path;//要下载的地址private int start;//起始位置private int end;//结束位置private static final String filedz = "C:\\xx.exe";@Overridepublic void run() {try {//创建一个URL对象URL url = new URL(path);//打开连接,获取了url请求的连接对象connHttpURLConnection conn = (HttpURLConnection) url.openConnection();//请求设置//请求的超时时间//            //读取超时时间//            //设置请求提交的方法Get Post//            //设置线程本次断点续传文件范围//            conn.setRequestProperty("Range","bytes="+start+"-"+end);//获取响应码conn.setConnectTimeout(5000);//请求的超时时间conn.setReadTimeout(5000);//读取超时时间conn.setRequestMethod("GET");//设置请求提交的方法Get Post//获取响应码int responseCode = conn.getResponseCode();if(responseCode==206){//由于是部分数据 206就表示ok//下载本段文件(参考课堂案例下载代码)InputStream in = conn.getInputStream();//获取用于读取网络资源的输入流//下载的流String fileName = conn.getHeaderField("Content-Disposition");//获取响应的头部文件(包含由文件名)fileName = fileName.split(";")[1];fileName = fileName.substring(fileName.indexOf("\"")+1,fileName.lastIndexOf("\""));//System.out.println(fileName);//成功获取文件名File file = new File(fileName);RandomAccessFile raf = new RandomAccessFile(file,"rwd");//可读、可写,有权限//读一组,下载一组byte[]bytes=new byte[1024];int len = 0;while((len=in.read(bytes))!=-1){raf.write(bytes,0,len);}//关闭流释放资源raf.close();in.close();//线程下载完毕System.out.println(this.getName()+":负责的"+start+"-"+end+" 下载完毕..");}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public String getPath() {return path;}public void setPath(String path) {this.path = path;}public int getStart() {return start;}public void setStart(int start) {this.start = start;}public int getEnd() {return end;}public void setEnd(int end) {this.end = end;}public static String getFiledz() {return filedz;}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部