使用curl断点续传下载文件
办公网络网速不是很好,使用Chrome下载一些软件时不时会中断,恶心的是Chrome居然不支持断点续传下载(为什么chrome的下载不支持断点续传呢?),迅雷自然是不能装的,那怎么办?还好我有大名鼎鼎的curl,看官网的介绍,是不是有点6到没朋友~~
Supports...
DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, HTTP/3, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, SCRAM-SHA, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.
What's curl used for?
curl is used in command lines or scripts to transfer data. curl is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the Internet transfer engine for thousands of software applications in over ten billion installations.
curl is used daily by virtually every Internet-using human on the globe.
而且 Windows 10 已经内置支持curl(Tar and Curl Come to Windows), 通过 PowerShell/CMD 可以直接使用,一口气上六楼,很方便!
C:\>curl
curl: try 'curl --help' for more information
C:\>curl -V -v
curl 7.55.1 (Windows) libcurl/7.55.1 WinSSL
Release-Date: 2017-11-14, security patched: 2019-11-05
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL
言归正传,下面我们使用酷酷的`curl`下载一些文件(注:以下演示均在高速网络下进行)
最简单的使用
curl --remote-name(-O) URL或者curl --output(-o) fileName URL, 以下载最新64位windows版本Curl为例C:\>mkdir Software C:\>cd Software C:\Software>curl -O https://curl.se/windows/dl-7.77.0_2/curl-7.77.0_2-win64-mingw.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 5154k 100 5154k 0 0 5154k 0 0:00:01 --:--:-- 0:00:01 6467k
使用
-continue-at(-C)断点续传下载,这次用PowerShell下载一个大文件(SSMS),关于该选项的使用,参见以下用户手册注解-C, --continue-at
Use "-C -" to tell curl to automatically find out where/how to resume the transfer. It then uses the given output/input files to figure that out.
在Powershell中使用需要带上后缀名,即
curl.exe,不然不能正确识别PS C:\Software> curl -C - -O https://download.microsoft.com/download/4/6/8/4681f3b2-f327-4d3d-8617-264b20685be0/SSMS-Setup-ENU.exe
Invoke-WebRequest : 无法处理参数,因为参数名称“C”具有二义性。可能的匹配项包括: -Credential -CertificateThumbprint -Certificate -ContentType。
所在位置 行:1 字符: 6
+ curl -C - -O https://download.microsoft.com/download/4/6/8/4681f3b2-f ...
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest],ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.InvokeWebRequestCommand PS C:\Software> curl --continue-at - -O https://download.microsoft.com/download/4/6/8/4681f3b2-f327-4d3d-8617-264b20685be0/SSMS-Setup-ENU.exe
Invoke-WebRequest : 找不到接受实际参数“-”的位置形式参数。
所在位置 行:1 字符: 1
+ curl --continue-at - -O https://download.microsoft.com/download/4/6/8 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest],ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
通过关闭无线/有线网络,模拟网络不稳定的情况
PS C:\Software> curl.exe -C - -O https://download.microsoft.com/download/4/6/8/4681f3b2-f327-4d3d-8617-264b20685be0/SSMS-Setup-ENU.exe
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
16 565M 16 94.7M 0 0 4852k 0 0:01:59 0:00:20 0:01:39 0
curl: (56) Send failure: Connection was reset PS C:\Software> curl.exe -C - -O https://download.microsoft.com/download/4/6/8/4681f3b2-f327-4d3d-8617-264b20685be0/SSMS-Setup-ENU.exe
** Resuming transfer from byte position 172521856
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
9 470M 9 46.9M 0 0 1502k 0 0:05:20 0:00:32 0:04:48 3720k
curl: (56) Send failure: Connection was reset
可以使用
--retry选项让下载失败后自动进行(此时网络没连接,在观察到重试现象后,打开网络连接)PS C:\Software> curl.exe -C - -O --retry 10 https://download.microsoft.com/download/4/6/8/4681f3b2-f327-4d3d-8617-264b20685be0/SSMS-Setup-ENU.exe
** Resuming transfer from byte position 221771295
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: Transient problem: timeout Will retry in 1 seconds. 10 retries left.
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: Transient problem: timeout Will retry in 2 seconds. 9 retries left.
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: Transient problem: timeout Will retry in 4 seconds. 8 retries left.
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: Transient problem: timeout Will retry in 8 seconds. 7 retries left.
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: Transient problem: timeout Will retry in 16 seconds. 6 retries left.
100 423M 100 423M 0 0 4667k 0 0:01:33 0:01:33 --:--:-- 7028k
PS C:\Software>
下载完成后使用断点续传及重试选项,不会覆盖已下载的文件;反之,则不成立
PS C:\Software> curl.exe -C - -O https://download.microsoft.com/download/4/6/8/4681f3b2-f327-4d3d-8617-264b20685be0/SSMS-Setup-ENU.exe
** Resuming transfer from byte position 666228072
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 237 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume. PS C:\Software> curl.exe -C - -O --retry 3 https://download.microsoft.com/download/4/6/8/4681f3b2-f327-4d3d-8617-264b20685be0/SSMS-Setup-ENU.exe
** Resuming transfer from byte position 666228072
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 237 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
Warning: Transient problem: FTP error Will retry in 1 seconds. 3 retries left.
0 237 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
Warning: Transient problem: FTP error Will retry in 2 seconds. 2 retries left.
0 237 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
Warning: Transient problem: FTP error Will retry in 4 seconds. 1 retries left.
0 237 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume. PS C:\Software> curl.exe -O https://download.microsoft.com/download/4/6/8/4681f3b2-f327-4d3d-8617-264b20685be0/SSMS-Setup-ENU.exe
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 635M 100 635M 0 0 16.2M 0 0:00:39 0:00:39 --:--:-- 24.2M
对于有些资源,网速不好的时候,甚至跳转到具体下载链接都很困难,以下载最新的 windows 64 位版本的 PostgreSQL 13.X为例,官网找到下载资源列表,页面跳转后一直不开始下载, 点击下载链接也没反应。。。

别捉急,curl一样可以帮到你。鼠标右键复制下载链接,使用--head(-I) 或者 --include(-i)选项(也可使用--verbose(-v)选项,这样,你可以看到所谓的TCP三次握手是怎样发生的),具体下载链接在Location首部字段里,然后使用上面提到的断点续传,DONE!
C:\>curl --head https://sbp.enterprisedb.com/getfile.jsp?fileid=1257713
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Server: nginx/1.16.1
Date: Sat, 10 Jul 2021 08:50:09 GMT
Location: https://get.enterprisedb.com/postgresql/postgresql-13.3-2-windows-x64.exe
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-Cache: Miss from cloudfront
Via: 1.1 a43db2746d5ea9543e11897b6654f9b6.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: HKG62-C1
X-Amz-Cf-Id: KuxMKejZWH-BMtFbzdKMCXsJE1ctLNMoSIcSYHLIkWQmbFnbexDsNg==
写在最后,准备这篇随笔的时候,翻阅了不少资料,发现有两个资源是很不错的, 一个是官网的教程, 另一个是官网提到的Everything curl,Everything curl另有中文翻译版, 找到下载章节,一个感觉就是:我有必要写这篇博客吗?写个寂寞。。。最后,附上原文地址, 原创不易,且行且珍惜~~
使用curl断点续传下载文件的更多相关文章
- curl -O 下载文件
curl -O 下载文件 学习了:http://blog.csdn.net/wulong710/article/details/53127606 curl -O http://a.b.c/a.tar ...
- Linux curl 命令下载文件
引用自http://blog.csdn.net/wh211212/article/details/54285921 命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工 ...
- 反射机制及开源框架xUitls的使用,使用HttpUtils通过断点续传下载文件
反射: Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它的任意一个方法和属性 Java反射机制主要提供下面几种用途: 1•在运行时判断 ...
- android多线程断点续传下载文件
一.目标 1.多线程抢占服务器资源下载. 2.断点续传. 二.实现思路. 假设分为三个线程: 1.各个线程分别向服务器请求文件的不同部分. 这个涉及Http协议,可以在Header中使用Range参数 ...
- python实现断点续传下载文件
最近的任务里有一个功能是要我从日志服务器实时跟新日志到本地,日志在不断新增内容就需要我隔一段时间从上次下载的位置继续下载,并写入本地文件上次写完的位置后面. headers = {'Range': ' ...
- Mac OS 终端下使用 Curl 命令下载文件
在 mac os下,如何通过命令行来下载网络文件?如果你没有安装或 wget 命令,那么可以使用 curl 工具来达到我们的目的. curl命令参数: curl 'url地址' curl [选项] ' ...
- 新技能 get —— Python 断点续传下载文件
from urllib.request import urlretrieve import sys import os prev_reported_download_percent = None # ...
- curl强制下载文件
<?phpfunction download_remote_file_with_curl($file_url, $save_to) { $ch = curl_init(); curl_setop ...
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
AsyncHttpClient开源框架android-async-http还是非常方便的. AsyncHttpClient该类通经常使用在android应用程序中创建异步GET, POST, PUT和 ...
随机推荐
- Docker——JVM 感知容器的 CPU 和 Memory 资源限制
前言 对于那些在Java应用程序中使用Docker的CPU和内存限制的人来说,可能会遇到一些挑战.特别是CPU限制,因为JVM在内部透明地设置GC线程和JIT编译器线程的数量. 这些可以通过命令行选项 ...
- RAM与FLASH
以前一直使用STM32但是对 变量 或 函数 的存储域没做任何了解:只知道你需要存储的东西就放在Flash的后面几页就好了:这次接触到STM8发现编译器里面有特别的存储查看器就打算看看到底是怎么存储的 ...
- HEX与ASCII之间装换
static const char bcd_ascll[20]={0x30,0x31,0x32,0x33,0x34, 0x35,0x36,0x37,0x38,0x39, ...
- 10.5 arp:管理系统的arp缓存
arp命令 用于操作本机的arp缓存区,它可以显示arp缓存区中的所有条目.删除指定的条目或者添加静态的IP地址与MAC地址的对应关系. 什么是arp?即地址解析协议(ARP,Address ...
- 浅谈:@RequestBody,@ResponseBody 和 @RequestParam
1. @RequestBody @RequestBody:主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据) GET方式无请求体,所以使用@RequestBody接收数据时,前端不 ...
- 炫彩流光按钮 CSS + HTML
炫彩流光按钮 写在前面 你若要喜爱你自己的价值,你就得给世界创造价值.--歌德 效果图 三个绝美的样例 HTML代码 <div class="box"> <but ...
- Windows 10, version 21H1 ARM64
请访问原文链接:https://sysin.org/article/windows-10-arm/,查看最新版.原创作品,转载请保留出处. Windows 10, version 21H1 (rele ...
- python工业互联网应用实战17—前后端分离模式之django template vs jquery3
上一章节我们完成了"CRUD"的后面3个功能点,新增由于改动较大我们专门增加本章来阐述,主要是完成技术栈切换后,会发现模板的代码判断过多,逻辑过于复杂.对未来存在的扩展和维护友好性 ...
- Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹
Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹 1.将一个文件夹下的所有内容复制到另一个文件夹下 cp -r /home/packageA/* /home/cp/packageB ...
- javascript数组排序之冒泡排序
冒泡排序 作为一名程序员数组的排序算法是必须要掌握的,今天来说最简单的一种数组排序----冒泡排序 冒泡排序原理 冒泡排序算法是一种简单直观的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如 ...