English 中文(简体)
如何使用标题“ Range ” 发送文件部件?
原标题:How to send file in parts using header "Range"?

I would like to send big file by dividing it to small parts and send them separately. I tried to use the hedder "Range" and got "org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity".

// create authenticate client
DefaultHttpClient client = new DefaultHttpClient();

// create HTTP put with the file
HttpPut httpPut = new HttpPut(url);
final File recordingFile = new File(mDir, mName);
long fileLength = recordingFile.length();
for (int i=0; i < fileLength; i += 4096) {
    int length = Math.min(4096, (int)recordingFile.length() - i);
    InputStreamEntity entity = new InputStreamEntity(inputStream, length);
    httpPut.setEntity(entity);
    httpPut.addHeader("Connection", "Keep-Alive");
    httpPut.addHeader("Range", "bytes=" + i + "-" + (i + length));

    // Execute
    HttpResponse res = client.execute(httpPut);
    int statusCode = res.getStatusLine().getStatusCode();
}

我还试了"Content-Range"头(而不是"Range"),我也有同样的例外。

httpPut.addHeader("Content-Range", "bytes=" + i + "-" + (i + length) + "/" + fileLength);
httpPut.addHeader("Accept-Ranges", "bytes");
最佳回答

You repeatedly send multiple of 4096 bits. E.g. let s take the first two steps: i = 0 Send range 0-4096 i = 4096 Send range 4096-8192.

修正此行 :

for (int i=0; i <= fileLength; i += 4097) {
    int length = Math.min(4096, (int)recordingFile.length() - i + 1);
    /*...*/
}

并且应该很好地工作。

Update: Maybe the problem is that for some reasons (e.g. authentication failure) it tries to resend the same chunk again, in which case the inputstream is already consumed. Try using a ByteArrayEntity instead of InputStreamEntity, something like this:

ByteArrayInputStream bis = new ByteArrayInputStream(recordingFile);
for (int i=0; i <= fileLength; i += 4097) {
    int length = Math.min(4096, (int)recordingFile.length() - i + 1);
    byte[] bytes = new byte[length];
    bis.read(bytes);
    ByteArrayEntity entity = ByteArrayEntity(bytes);
    /*...*/
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签