打印 上一主题 下一主题 利用cURL实现单个文件分多段同时下载,支持断点续传(修订版)
![]() 利用cURL实现单个文件分多段同时下载,支持断点续传(修订版) [复制链接] |
摘自 http://bbs.chinaunix.net/thread-917952-1-1.html
在ubuntu下测试通过, 适合在支持多线程下载的站点下载文件
可以配合flashgot在firefox中使用
用法:./mycurl url [referedUrl]
第一个参数url是要下载的文件的地址,第二个参数referedUrl是指需要参照的网址(一般不需要,有些网站,比如华军需要此参数)
例如:
./mycurl ftp://xx.xxx.xxx/xxx.rar
或者
./mycurl http://xx.xxx.xx/xxx.rar http://www.xxx.xxx/yy.htm
下面是代码:
#!/bin/bash
####################################################################
#
# Script for curl to support resumable multi-part download.
#
# Tested on Ubuntu
#
url=$1
# How many "parts" will the target file be divided into?
declare -i parts=5
read -ep "Please input the target directory: " targetdir
read -ep "Please input the outfile name: " outfile
[ -z "$targetdir" ] && targetdir="./"
cd $targetdir||exit 2
[ -z "$outfile" ] && outfile=`basename $1`
#Set the referer url
if [ -n "$2" ]; then
refurl="-L -e $2"
else refurl=""
fi
length=`curl $refurl -s -I $url|grep Content-Length|tail -n 1|sed s/[^0-9]//g`
if [ -z "$length" ]; then
echo "cann't get the length of the target file"
exit 1
fi
let "length = $length"
#lsession is used to record how many bytes of each subpart should be downloaded
declare -i lsession=$(($length/$parts))
finished="false"
#Assume the available maximum connections on server can reach "parts" at first
maxconn=$parts
while true;
do
for (( i=1; i<=parts ; i=i+1 ))
do
#Array offsetold is used to record how many bytes have been downloaded of each subpart
if [ -e $outfile$i ]; then
offsetold[$i]=`ls -l $outfile$i|awk '{print $5}'`
else offsetold[$i]=0
fi
let "offsetold[$i] = ${offsetold[$i]}"
done
curr=0
for (( i=1; i<=parts && maxconn>0; i=i+1 ))
do
if [ $i -lt $parts ]; then
if [ ${offsetold[$i]} -lt $lsession ]; then
curl $refurl -r $(($curr+${offsetold[$i]}))-$(($curr+$lsession-1)) $url >> $outfile$i &
maxconn=$(($maxconn-1))
fi
else
if [ ${offsetold[$i]} -lt $(($length-$(($lsession*$(($parts-1)))))) ]; then
curl $refurl -r $(($curr+${offsetold[$i]}))- $url >> $outfile$i &
maxconn=$(($maxconn-1))
fi
fi
curr=$(($curr+$lsession))
done
#To wait for all curl processes to terminate.
wait
finished="true"
maxconn=0
for (( i=1; i<=parts; i=i+1 ))
do
#Array offsetnew is used to record how many bytes have been downloaded of each subpart
if [ -e $outfile$i ]; then
offsetnew[$i]=`ls -l $outfile$i|awk '{print $5}'`
else offsetnew[$i]=0
fi
let "offsetnew[$i] = ${offsetnew[$i]}"
if [ $i -lt $parts ]; then
if [ ${offsetnew[$i]} -lt $lsession ]; then
finished="false"
fi
else
if [ ${offsetnew[$i]} -lt $(($length-$(($lsession*$(($parts-1)))))) ]; then
finished="false"
fi
fi
#Calculate the "real" available maximum connections supported by server
if [ ${offsetnew[$i]} -gt ${offsetold[$i]} ]; then
maxconn=$(($maxconn+1))
fi
done
if [ "$finished" == "true" ]; then
break
elif [ $maxconn -eq 0 ]; then
echo "Some errors may occur. retry 10 sec later..."
sleep 10
maxconn=parts
fi
done
echo "All parts have been downloaded. Merging..."
mv --backup=t $outfile"1" $outfile
for (( i=2; i<=parts; i=i+1))
do
cat $outfile$i >> $outfile
rm $outfile$i
done
echo "Done."
[ 本帖最后由 ypxing 于 2007-4-4 21:45 编辑 ]
|
|
|
|
|
2楼
|
|
| SUN E4500/SUN F4800/SUN V880 Solaris 8 KSH/NAWK/SED/VIM 6.3.3/perl 5.005_03 |
||

- 论坛徽章:
- 0
3楼
回复 2楼 一梦如是 的帖子
|
谢谢大侠指点, 学习中... 俺也去试试axel |
钢七连
不抛弃,不放弃
http://ypxing.cublog.cn/
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
4楼
|
|
| 慷慨陈词,岂能皆如人意,鞠躬尽瘁,但求无愧我心。 stay hungry, stay foolish https://github.com/tcler http://www.tldp.org/LDP/abs/html |
||

- 论坛徽章:
- 0
5楼
| 哇,支持! |
爱家、爱国、爱和平、爱自由、爱生活、爱大自然!
|
|
6楼
|
|

- 论坛徽章:
- 0
7楼
|
一开始写这个script是为了和flashgot配合,在firefox里使用来者,呵呵 我也经常用wget的
|
钢七连
不抛弃,不放弃
http://ypxing.cublog.cn/
|
|
8楼
|
| ASUSW3Z-W3HT30; DELL Lattitude D630; ThinkPadX61 7675C ; DELL OPTIPLEX-755 | |

- 论坛徽章:
- 0
9楼
|
谢谢 用他们提供的库可以开发更友好,功能更复杂的东东,有时间要试一下的
|
钢七连
不抛弃,不放弃
http://ypxing.cublog.cn/
|
|
10楼
|
|
| 钢七连 不抛弃,不放弃 http://ypxing.cublog.cn/ |
打印 上一主题 下一主题 利用cURL实现单个文件分多段同时下载,支持断点续传(修订版)的更多相关文章
- 利用struts2进行单个文件,批量文件上传,ajax异步上传以及下载
利用struts2进行单个文件,批量文件上传,ajax异步上传以及下载 1.页面显示代码 <%@ page language="java" import="java ...
- 【FTP】FTP文件上传下载-支持断点续传
Jar包:apache的commons-net包: 支持断点续传 支持进度监控(有时出不来,搞不清原因) 相关知识点 编码格式: UTF-8等; 文件类型: 包括[BINARY_FILE_TYPE(常 ...
- 文件的上传(可以上传照片,word文档,等单个文件)
jsp: jsp页面: <LINK href="${basePath}plugins/uploadify/uploadify.css" type="text/css ...
- 苹果电脑利用curl下载数据集
在看tensorflow书上迁徙学习的这一部分的时候,书上说利用 curl http://download.tensorflow.org/example_images/flower_photos.tg ...
- cesium结合geoserver利用WFS服务实现图层编辑(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- cesium结合geoserver利用WFS服务实现图层删除(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- Pacman主题下给Hexo增加简历类型
原文 http://blog.zanlabs.com/2015/01/02/add-resume-type-to-hexo-under-pacman-theme/ 背景 虽然暂时不找工作,但是想着简历 ...
- Hexo快速构建个人小站-Fulid主题下添加Valine评论系统(三)
Hexo目录: Hexo快速构建个人小站-Hexo初始化和将项目托管在Github(一) Hexo快速构建个人小站-自定义域名和自定义主题(二) 背景交代: 前面两章完成了Hexo的初始化和部分自定义 ...
- Java: 在不同windows主题下,JFrame窗口设置最佳高度的解决方案
//设置窗口的大小,无论使用怎样的windows主题,都能灵活的应对,显示合适的窗口大小,一定要在JFrame.setVisible(true)之前调用, //替代传统的frame.setSize(w ...
随机推荐
- 开源欣赏wordpress之用户新增user-new.php
require_once( dirname( __FILE__ ) . '/admin.php' ); 引入根文件. if ( is_multisite() ) { if ( ! current_us ...
- codecomb 2090【最小乘积路】
题目描述 给定n个点的带权有向图,求从1到n的路径中边权之积最小的简单路径. 输入格式 第一行读入两个整数n,m,表示共n个点m条边. 接下来m行,每行三个正整数x,y,z,表示点x到点y有一条边权为 ...
- Climbing Stairs 解答
Question You are climbing a stair case. It takes n steps to reach to the top. Each time you can eith ...
- Java如何访问Axis2服务端
import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.address ...
- hdu 2438 Turn the corner(几何+三分)
Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...
- Python递归函数与斐波那契数列
定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 阶乘实例 n = int(input(">>:")) def f(n): s ...
- java面试题集1
一:单选题 下列哪一种叙述是正确的(D )A. abstract修饰符可修饰字段.方法和类B. 抽象方法的body部分必须用一对大括号{ }包住C. 声明抽象方法,大括号可有可无D. 声明抽象方法不可 ...
- iOS https认证 && SSL/TLS证书申请
1.下面列出截止2016年底市面上常见的免费CA证书: 腾讯云SSL证书管理(赛门铁克TrustAsia DV SSL证书)阿里云云盾证书服务(赛门铁克DV SSL证书)百度云SSL证书服务Let's ...
- lua 类实现
Class={}; Class.classList={}; --保存所有已经定义过的类 --类的类型: 类和接口, 接口也是一种类 Class.TYPE_CLASS="Class" ...
- 左右HttpClient上传的方法来解决中国的乱码
二手HttpClient人们都知道通过addTextBody方法来加入要上传的文本信息,可是,假设要上传中文的话.或还有中文名称的文件会出现乱码的问题,解决的方法事实上非常easy: 第一步:设置Mu ...







