我正在尝试从吱吱声下载文件到磁盘。我的方法适用于小文本/html文件,但由于缺乏缓冲,对于大型二进制文件来说非常慢https://mirror.racket-lang.org/installers/6.12/racket-6.12-x86_64-win32.exe。此外,完成后,文件要大得多(113 MB(比下载页面上显示的要多 (75MB(。
我的代码如下所示:
download: anURL
"download a file over HTTP and save it to disk under a name extracted from url."
| ios name |
name := ((anURL findTokens: '/') removeLast findTokens: '?') removeFirst.
ios := FileStream oldFileNamed: name.
ios nextPutAll: ((HTTPClient httpGetDocument: anURL) content).
ios close.
Transcript show: 'done'; cr.
我已经尝试使用 [stream atEnd] whileFalse:
循环在 HTTP 响应的contentStream
中对固定大小的块进行[bytes = stream next bufSize. bytes printTo: ios]
,但这会使输出文件在每个块周围带有单引号,并且在块之后还有额外的内容,看起来像流的所有字符,每个都用单引号引起来。
如何实现对磁盘文件的 HTTP 响应的缓冲写入?另外,有没有办法在显示下载进度时发出吱吱声?
正如莱安德罗已经写的那样,问题出在#binary
.
您的代码几乎是正确的,我冒昧地运行了它 - 现在它正确下载了整个文件:
| ios name anURL |
anURL := ' https://mirror.racket-lang.org/installers/6.12/racket-6.12-x86_64-win32.exe'.
name := ((anURL findTokens: '/') removeLast findTokens: '?') removeFirst.
ios := FileStream newFileNamed: 'C:UsersuserDownloads_squeak', name.
ios binary.
ios nextPutAll: ((HTTPClient httpGetDocument: anURL) content).
ios close.
Transcript show: 'done'; cr.
至于冻结,我认为问题出在您下载时整个环境的一个线程上。这意味着在您下载整个文件之前,您将无法使用Squeak。
刚刚在 Pharo 中测试(更容易安装(,以下代码可以根据需要工作:
ZnClient new
url: 'https://mirror.racket-lang.org/installers/6.12/racket-6.12-x86_64-win32.exe';
downloadTo: 'C:UsersuserDownloads_squeak'.
WebResponse
类在构建响应内容时,会创建一个足够大的缓冲区来容纳整个响应,即使对于巨大的响应也是如此!我认为这是由于WebMessage>>#getContentWithProgress:
中的代码而发生的。
我尝试将数据从WebResponse
的输入SocketStream
直接复制到输出FileStream
。我不得不对WebClient
和WebResponse
进行子类化,并编写两个方法。现在,以下代码根据需要工作。
| client link |
client := PkWebClient new.
link := 'http://localhost:8000/racket-6.12-x86_64-linux.sh'.
client download: link toFile: '/home/yo/test'.
我已经逐块验证了下载文件的更新和完整性。
我在下面包括来源。streamContentDirectToFile: aFilePathString
的方法是以不同的方式做事并解决问题的方法。
WebClient subclass: #PkWebClient
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'PK'!
!PkWebClient commentStamp: 'pk 3/28/2018 20:16' prior: 0!
Trying to download http directly to file.!
!PkWebClient methodsFor: 'as yet unclassified' stamp: 'pk 3/29/2018 13:29'!
download: urlString toFile: aFilePathString
"Try to download large files sensibly"
| res |
res := self httpGet: urlString.
res := PkWebResponse new copySameFrom: res.
res streamContentDirectToFile: aFilePathString! !
WebResponse subclass: #PkWebResponse
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'PK'!
!PkWebResponse commentStamp: 'pk 3/28/2018 20:49' prior: 0!
To make getContentwithProgress better.!
]style[(38)f1!
!PkWebResponse methodsFor: 'as yet unclassified' stamp: 'pk 3/29/2018 13:20'!
streamContentDirectToFile: aFilePathString
"stream response's content directly to file."
| buffer ostream |
stream binary.
buffer := ByteArray new: 4096.
ostream := FileStream oldFileNamed: aFilePathString.
ostream binary.
[stream atEnd]
whileFalse: [buffer := stream nextInBuffer: 4096.
stream receiveAvailableData.
ostream nextPutAll: buffer].
stream close.
ostream close! !