Java:在 URLConnection 中恢复下载

发布于 2024-09-12 15:09:24 字数 1382 浏览 6 评论 0原文

我编写了一个程序,从某些服务器下载一些文件。
目前程序运行正常。
但我想为其添加简历支持。
我这样做但是结果文件已损坏:

....

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2024-09-19 15:09:24

尝试:

connection.setRequestProperty("Range", "bytes=" + fcheck.length() + "-");

根据规范将范围说明符小写。另外,如果您的部分文件为 500 字节,则意味着您拥有的字节范围是 0-499,而您需要 500+。

Try:

connection.setRequestProperty("Range", "bytes=" + fcheck.length() + "-");

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+.

鹤舞 2024-09-19 15:09:24

问题出在(fcheck.length() - 1):这应该是fcheck.length()

The problem is in (fcheck.length() - 1): this should be fcheck.length().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文