我使用Url("some_url").openStream()
或OkHttp的response.body().byteStream()
从http请求接收InputStream。目的是将InputStream逐块读取到字节数组缓冲区,并将其发送到支持可恢复上传的API方法。
我已经做了一些实现,但是最终文件损坏了(存档无法扩展,视频无法播放等)。
int UPLOAD_THRESHOLD = 1024 * 1024 * 5; // 5 MB
int uploadedSize = 0;
while ((uploadedSize + UPLOAD_THRESHOLD) < docSize) {
Response<ResponseBody> response = fileService.updateResumableFileContent(token,
fileId, false, uploadedSize, getChunkBytes(inputStream, UPLOAD_THRESHOLD));
if (response.isSuccessful()) {
uploadedSize = uploadedSize + UPLOAD_THRESHOLD;
}
}
Response<ResponseBody> response = fileService.updateResumableFileContent(token,
fileId, true, uploadedSize, getChunkBytes(inputStream, UPLOAD_THRESHOLD));
if (response.isSuccessful()) {
return getDocument(fileId);
}
private byte[] getChunkBytes(InputStream inputStream, int offset) throws IOException {
byte[] buffer = new byte[offset];
int readCount = inputStream.read(buffer);
if (readCount == -1) {
throw new IOException();
}
return buffer;
}