Java:在 URLConnection 中恢复下载
我编写了一个程序,从某些服务器下载一些文件。
目前程序运行正常。
但我想为其添加简历支持。
我这样做但是结果文件已损坏:
....
File fcheck=new File(SaveDir+"/"+filename);
if(resumebox.isSelected() && fcheck.exists()){
connection.setRequestProperty("Range", "Bytes="+(fcheck.length())+"-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
BufferedInputStream in = new BufferedInputStream (connection.getInputStream());
pbar.setIndeterminate(false);
pbar.setStringPainted(true);
java.io.FileOutputStream fos ;
if(resumebox.isSelected()){
if(fcheck.exists()){
if(connection.getHeaderField("Accept-Ranges").equals("bytes")){
fos = new java.io.FileOutputStream(SaveDir+"/"+filename,true);
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
....
我正在我知道支持恢复的服务器上测试它。
我下载了一些字节。(72720)
然后尝试恢复它。
然后我用十六进制编辑器打开文件,在偏移量 72720 处重复第一个字节:
字节 0-36:FLV.............<.................onMetaData
字节 72720-72756:FLV........................«.................onMetaData
它从头开始下载!
当我通过 wget 执行此操作时,它会正确执行并通过 Content-Range 字段进行响应!
服务器在 wget 日志中响应“302 FOUND”和“206 Partial Content”。
“302 FOUND”会导致问题吗?
有什么问题吗?
谢谢。
I wrote a program that downloads some files from some servers.
Currently program works properly.
But I want to add resume support to it.
I'm doing it like this But the result file is corrupted:
....
File fcheck=new File(SaveDir+"/"+filename);
if(resumebox.isSelected() && fcheck.exists()){
connection.setRequestProperty("Range", "Bytes="+(fcheck.length())+"-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
BufferedInputStream in = new BufferedInputStream (connection.getInputStream());
pbar.setIndeterminate(false);
pbar.setStringPainted(true);
java.io.FileOutputStream fos ;
if(resumebox.isSelected()){
if(fcheck.exists()){
if(connection.getHeaderField("Accept-Ranges").equals("bytes")){
fos = new java.io.FileOutputStream(SaveDir+"/"+filename,true);
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
}else{
fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}
....
I'm Testing it on a server that I know supports resume.
I downloaded some bytes.(72720)
Then Tried to resume it.
Then I opened file with a Hex editor , At offset 72720 the first Bytes are repeated:
Bytes 0-36: FLV.............«..........onMetaData
Bytes 72720-72756: FLV.............«..........onMetaData
It Starts download from the begining!
While when I do it by wget it does correctly and responses by Content-Range field!
Server responses with "302 FOUND" and a "206 Partial Content" in wget log.
Can "302 FOUND" cause the problem?
What is the problem ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
根据规范将范围说明符小写。另外,如果您的部分文件为 500 字节,则意味着您拥有的字节范围是 0-499,而您需要 500+。
Try:
Lowercase the range specifier per the spec. Also, if your partial file was 500 bytes, that means your byte range that you have is 0-499, and you want 500+.
问题出在
(fcheck.length() - 1)
:这应该是fcheck.length()
。The problem is in
(fcheck.length() - 1)
: this should befcheck.length()
.