4

我正在尝试使用 setRequestProperty("Range","bytes=" + startbytes + "-" + endbytes); 以下代码片段显示了我正在尝试做的事情。

protected String doInBackground(String... aurl) {
    int count;
    Log.d(TAG,"Entered");
    try {

        URL url = new URL(aurl[0]);
        HttpURLConnection connection =(HttpURLConnection) url.openConnection();

        int lengthOfFile = connection.getContentLength();

        Log.d(TAG,"Length of file: "+ lengthOfFile);

        connection.setRequestProperty("Range", "bytes=" + 0 + "-" + 1000);

问题是,出现了一个异常,上面写着“连接后无法设置请求属性”。请帮我解决这个问题。

4

2 回答 2

7

选项1

如果您不需要知道内容长度:

[注意,不要调用connection.getContentLength(). 如果你调用它,你会得到异常。如果你需要调用它,那么勾选第二个选项]

URL url = new URL(aurl[0]);
HttpURLConnection connection =(HttpURLConnection) url.openConnection();
connection.setRequestProperty("Range", "bytes=" + 0 + "-" + 1000);
//Note that, response code will be 206 (Partial Content) instead of usual 200 (OK)
if(connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL){
    //Your code here to read response data
}

选项 2

如果您需要知道内容长度:

URL url = new URL(aurl[0]);
//First make a HEAD call to get the content length  
HttpURLConnection connection =(HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
    int lengthOfFile = connection.getContentLength();
    Log.d("ERF","Length of file: "+ lengthOfFile);
    connection.disconnect();

    //Now that we know the content lenght, make the GET call
    connection =(HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Range", "bytes=" + 0 + "-" + 1000);
    //Note that, response code will be 206 (Partial Content) instead of usual 200 (OK)
    if(connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL){
        //Your code here to read response data

    }
}
于 2013-05-28T09:34:09.027 回答
3

假设您使用 HTTP 进行下载,您将需要使用 HEAD http 动词和 RANGE http 标头。

HEAD 会给你文件大小(如果有的话),然后 RANGE 让你下载一个字节范围。

获得文件大小后,将其分成大小大致相等的块,并为每个块生成下载线程。全部完成后,以正确的顺序写入文件块。

如果您不知道如何使用 RANGE 标头,这是另一个解释如何使用的 SO 答案:https ://stackoverflow.com/a/6323043/1355166

[编辑]

要将文件分成块,请使用它,并开始下载过程,

private void getBytesFromFile(File file) throws IOException {
    FileInputStream is = new FileInputStream(file); //videorecorder stores video to file

    java.nio.channels.FileChannel fc = is.getChannel();
    java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10000);

    int chunkCount = 0;

    byte[] bytes;

    while(fc.read(bb) >= 0){
        bb.flip();
        //save the part of the file into a chunk
        bytes = bb.array();
        storeByteArrayToFile(bytes, mRecordingFile + "." + chunkCount);//mRecordingFile is the (String)path to file
        chunkCount++;
        bb.clear();
    }
}

private void storeByteArrayToFile(byte[] bytesToSave, String path) throws IOException {
    FileOutputStream fOut = new FileOutputStream(path);
    try {
        fOut.write(bytesToSave);
    }
    catch (Exception ex) {
        Log.e("ERROR", ex.getMessage());
    }
    finally {
        fOut.close();
    }
}
于 2013-05-28T09:32:53.760 回答