打印 上一主题 下一主题 利用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 ...
随机推荐
- Invert Binary Tree 解答
Quetion Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Solution 1 -- R ...
- Course Schedule II 解答
Question There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may ...
- Minimum Depth of Binary Tree 解答
Question Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- C/C++误区四:char c = getchar();
许多初学者都习惯用 char 型变量接收 getchar.getc,fgetc 等函数的返回值,其实这么做是不对的,并且隐含着足以 致命的错误 .getchar 等函数的返回值类型都是 int 型 ...
- 456. 132 Pattern
456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...
- NGUI研究之制作转圈的技能CD特效
昨天想做一个技能CD转圈的特效,花了大把的时间去用meshRender组件想通过三角形依据数学算法来绘制一个圆形的网格.通过动态绘制圆形网格的方法来实现技能CD特效.奶奶的昨天我研究了一晚上,最 ...
- uploadify按钮中文乱码问题
uploadify是一款基于jQuery库的上传插件,但很可惜的是无论你怎么设置参数buttonText ,它的中文按钮都会出现乱码的情况,现把出现原因及解决方法总结如下. 那么出现这种的 ...
- 敏捷软件开发之TDD(一)
测试驱动开发即TDD是敏捷软件开发方法的重要组成部分.TDD是从极限编程中发展而来,它既可以用在设计时也可以用在开发实践中.TDD把业务需求转化为可以运行的测试代码并具有如下的优点1.TDD从一开始就 ...
- TCP/IP详解之:SNMP
基于TCP/IP的网络管理包含3个组成部分: 一个管理信息库MIB:MIB包含所有代理进程的所有可被查询和修改的参数 关于MIB的一套公用的结构和表示符号,即SMI(管理信息结构) 管理进程和代理进程 ...







