任务是将视频分段上传到服务器。我见过很多类似的问题,但是我需要手动发送具有指定字节范围的数据包才能停止和继续。我似乎错误地创建了标题和正文。
现在请求看起来像这样:
POST http://ovu.mycdn.me/upload.do?sig=... HTTP/1.1
Content-Range: bytes 196608-262144/4549847
Content-Type: multipart/mixed; boundary=66614565-7d65-44a3-a78e-49f6f9ee6d53
Content-Length: 65749
Host: ovu.mycdn.me
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/4.5.0
--666...f9ee6d53
Content-Disposition: form-data; name="video_file"; filename="video"
Content-Type: multipart/form-data
Content-Length: 65536
--666...f9ee6d53--
以及创建一个请求的代码。成功时,发送下一个范围。
RequestBody requestBody = new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("multipart/form-data");
}
@Override
public long contentLength() {
return finalBytesRead;
}
@Override
public void writeTo(@NotNull BufferedSink sink) throws IOException {
sink.write(buffer, 0, finalBytesRead); // write 64kb from file
}
};
RequestBody requestBody1 = new MultipartBody.Builder()
.addFormDataPart("video_file", "video", requestBody)
.build();
Request request = new Request.Builder()
.addHeader("Content-Range", "bytes " + currentLength + "-" + (currentLength + bytesRead) + '/' + size)
.addHeader("Accept-Ranges", "bytes")
.url(url)
.post(requestBody1)
.build();
call = client.newCall(request);
call.enqueue(new Callback() {
....
服务器可以以秒为单位查看视频大小,但视频无法打开。
视频应位于 video_file 字段中,格式为multipart/form-data