Java:resume在URLConnection中下载 [英] Java: resume Download in URLConnection

查看:113
本文介绍了Java:resume在URLConnection中下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个从某些服务器下载一些文件的程序。

目前程序正常运行。

但是我想为它添加简历支持。

我这样做但结果文件已损坏:

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);
}

....

我正在测试它在我知道支持简历的服务器上。

我下载了一些字节。(72720)

然后试图恢复它。

然后我打开文件十六进制编辑器,偏移72720,重复第一个字节:

字节0-36:FLV .............«.......... onMetaData

字节72720-72756:FLV .............«.......... onMetaData

它开始下载开始!

当我通过wget执行此操作时,它正确执行并通过Content-Range字段进行响应!

服务器响应中包含302 FOUND和206 Partial Content wget log。

302 FOUND会导致问题吗?

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.

推荐答案

尝试:

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

根据规范小写范围说明符。此外,如果你的部分文件是500字节,这意味着你的字节范围是0-499,你想要500 +。

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

这篇关于Java:resume在URLConnection中下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆