打印 上一主题 下一主题 利用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 ...
随机推荐
- 【转】 linux内核移植和网卡驱动(二)
原文网址:http://blog.chinaunix.net/uid-29589379-id-4708911.html 一,内核移植步骤: 1, 修改顶层目录下的Makefile ARCH ...
- 嵌入式设备web服务器
操作系统:ubuntu10.04 前言: 为了提高对设备的易操作性,很多设备中提供pc机直接通过浏览器操作设备的功能.这就需要在设备中实现web服务器. 现在在嵌入式设备中所使用的web服 ...
- LeeCode-Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- canvas动画文字效果
Doughnut Chartvar c=document.getElementById("canvas");var ctx=c.getContext("2d") ...
- SHDP--Working With HBase(一)之基本介绍
最近在做web项目使用到了Hadoop,HBase,在这里对Spring For Hadoop(SHDP)的使用做个总结,主要使用了SHDP中提供的一些封装好的HBase模块. Spring For ...
- 模块工具类--utils
File: js\utils.js/** * 模块工具类,用来初始化各模块视图.自定绑定事件以及其他辅助功能等 * @class Utils */Utils = (function() { var i ...
- FileUpload的使用案例
文件上传 1.www.apache.org下载commons fileupload 和 commons io 2.创建jsp并附上如下代码 <%@ page language="jav ...
- ios应用view之间数据传递的方式
对于不同的viewcontroller之间数据的共享和处理 采用代理的方式,子viewcontroller设计代理协议,并定义协议接口,父viewcontroller实现协议接口,实现子视图控制器退出 ...
- python 笔记2--函数
函数变量 >>> a = abs # 变量a指向abs函数 >>> a(-1) # 所以也可以通过a调用abs函数 1 定义函数 def my_abs(x): if ...
- 11i - 12 How To Set Email Style Preference For All Users At Once?
(文档 ID 578574.1) In this Document Goal Solution Workflow Information Center, Diagnostics, & ...