- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
是的,这是一个很长的问题,有很多细节... 所以,我的问题是:如何分段将上传内容流式传输到 Vimeo?
对于任何想要在自己的机器上复制和调试的人:以下是您需要的东西:
C:\test.mp4
或更改该代码以指向您所在的任何位置。 1001-339108/339108
什么时候应该1001-339107/339108
.所以...是的... /**
* Send the video data
*
* @return whether the video successfully sent
*/
private static boolean sendVideo(String endpoint, File file) throws FileNotFoundException, IOException {
// Setup File
long contentLength = file.length();
String contentLengthString = Long.toString(contentLength);
FileInputStream is = new FileInputStream(file);
int bufferSize = 10485760; // 10 MB = 10485760 bytes
byte[] bytesPortion = new byte[bufferSize];
int byteNumber = 0;
int maxAttempts = 1;
while (is.read(bytesPortion, 0, bufferSize) != -1) {
String contentRange = Integer.toString(byteNumber);
long bytesLeft = contentLength - byteNumber;
System.out.println(newline + newline + "Bytes Left: " + bytesLeft);
if (bytesLeft < bufferSize) {
//copy the bytesPortion array into a smaller array containing only the remaining bytes
bytesPortion = Arrays.copyOf(bytesPortion, (int) bytesLeft);
//This just makes it so it doesn't throw an IndexOutOfBounds exception on the next while iteration. It shouldn't get past another iteration
bufferSize = (int) bytesLeft;
}
byteNumber += bytesPortion.length;
contentRange += "-" + (byteNumber - 1) + "/" + contentLengthString;
int attempts = 0;
boolean success = false;
while (attempts < maxAttempts && !success) {
int bytesOnServer = sendVideoBytes("Test video", endpoint, contentLengthString, "video/mp4", contentRange, bytesPortion, first);
if (bytesOnServer == byteNumber) {
success = true;
} else {
System.out.println(bytesOnServer + " != " + byteNumber);
System.out.println("Success is not true!");
}
attempts++;
}
first = true;
if (!success) {
return false;
}
}
return true;
}
/**
* Sends the given bytes to the given endpoint
*
* @return the last byte on the server (from verifyUpload(endpoint))
*/
private static int sendVideoBytes(String videoTitle, String endpoint, String contentLength, String fileType, String contentRange, byte[] fileBytes, boolean addContentRange) throws FileNotFoundException, IOException {
OAuthRequest request = new OAuthRequest(Verb.PUT, endpoint);
request.addHeader("Content-Length", contentLength);
request.addHeader("Content-Type", fileType);
if (addContentRange) {
request.addHeader("Content-Range", contentRangeHeaderPrefix + contentRange);
}
request.addPayload(fileBytes);
Response response = signAndSendToVimeo(request, "sendVideo on " + videoTitle, false);
if (response.getCode() != 200 && !response.isSuccessful()) {
return -1;
}
return verifyUpload(endpoint);
}
/**
* Verifies the upload and returns whether it's successful
*
* @param endpoint to verify upload to
* @return the last byte on the server
*/
public static int verifyUpload(String endpoint) {
// Verify the upload
OAuthRequest request = new OAuthRequest(Verb.PUT, endpoint);
request.addHeader("Content-Length", "0");
request.addHeader("Content-Range", "bytes */*");
Response response = signAndSendToVimeo(request, "verifyUpload to " + endpoint, true);
if (response.getCode() != 308 || !response.isSuccessful()) {
return -1;
}
String range = response.getHeader("Range");
//range = "bytes=0-10485759"
return Integer.parseInt(range.substring(range.lastIndexOf("-") + 1)) + 1;
//The + 1 at the end is because Vimeo gives you 0-whatever byte where 0 = the first byte
}
/**
* Signs the request and sends it. Returns the response.
*
* @param service
* @param accessToken
* @param request
* @return response
*/
public static Response signAndSendToVimeo(OAuthRequest request, String description, boolean printBody) throws org.scribe.exceptions.OAuthException {
System.out.println(newline + newline
+ "Signing " + description + " request:"
+ ((printBody && !request.getBodyContents().isEmpty()) ? newline + "\tBody Contents:" + request.getBodyContents() : "")
+ ((!request.getHeaders().isEmpty()) ? newline + "\tHeaders: " + request.getHeaders() : ""));
service.signRequest(accessToken, request);
printRequest(request, description);
Response response = request.send();
printResponse(response, description, printBody);
return response;
}
contentRangeHeaderPrefix
的内容而变化设置为和
first
boolean 设置为(指定是否在第一个块上包含 Content-Range header )。
We're sending the video for upload!
Bytes Left: 15125120
Signing sendVideo on Test video request:
Headers: {Content-Length=15125120, Content-Type=video/mp4, Content-Range=bytes%200-10485759/15125120}
sendVideo on Test video >>> Request
Headers: {Authorization=OAuth oauth_signature="zUdkaaoJyvz%2Bt6zoMvAFvX0DRkc%3D", oauth_version="1.0", oauth_nonce="340477132", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="5cb447d1fc4c3308e2c6531e45bcadf1", oauth_token="460633205c55d3f1806bcab04174ae09", oauth_timestamp="1334336004", Content-Length=15125120, Content-Type=video/mp4, Content-Range=bytes: 0-10485759/15125120}
Verb: PUT
Complete URL: http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d
sendVideo on Test video >>> Response
Code: 200
Headers: {null=HTTP/1.1 200 OK, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}
Signing verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d request:
Headers: {Content-Length=0, Content-Range=bytes */*}
verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d >>> Request
Headers: {Authorization=OAuth oauth_signature="FQg8HJe84nrUTdyvMJGM37dpNpI%3D", oauth_version="1.0", oauth_nonce="298157825", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="5cb447d1fc4c3308e2c6531e45bcadf1", oauth_token="460633205c55d3f1806bcab04174ae09", oauth_timestamp="1334336015", Content-Length=0, Content-Range=bytes */*}
Verb: PUT
Complete URL: http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d
verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d >>> Response
Code: 308
Headers: {null=HTTP/1.1 308 Resume Incomplete, Range=bytes=0-10485759, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}
Body:
Bytes Left: 4639360
Signing sendVideo on Test video request:
Headers: {Content-Length=15125120, Content-Type=video/mp4, Content-Range=bytes: 10485760-15125119/15125120}
sendVideo on Test video >>> Request
Headers: {Authorization=OAuth oauth_signature="qspQBu42HVhQ7sDpzKGeu3%2Bn8tM%3D", oauth_version="1.0", oauth_nonce="183131870", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="5cb447d1fc4c3308e2c6531e45bcadf1", oauth_token="460633205c55d3f1806bcab04174ae09", oauth_timestamp="1334336015", Content-Length=15125120, Content-Type=video/mp4, Content-Range=bytes%2010485760-15125119/15125120}
Verb: PUT
Complete URL: http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d
sendVideo on Test video >>> Response
Code: 200
Headers: {null=HTTP/1.1 200 OK, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}
Signing verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d request:
Headers: {Content-Length=0, Content-Range=bytes */*}
verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d >>> Request
Headers: {Authorization=OAuth oauth_signature="IdhhhBryzCa5eYqSPKAQfnVFpIg%3D", oauth_version="1.0", oauth_nonce="442087608", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="5cb447d1fc4c3308e2c6531e45bcadf1", oauth_token="460633205c55d3f1806bcab04174ae09", oauth_timestamp="1334336020", Content-Length=0, Content-Range=bytes */*}
Verb: PUT
Complete URL: http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d
4639359 != 15125120
verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d >>> Response
Success is not true!
Code: 308
Headers: {null=HTTP/1.1 308 Resume Incomplete, Range=bytes=0-4639359, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}
Body:
Exception in thread "main" org.scribe.exceptions.OAuthException: Problems while creating connection.
at org.scribe.model.Request.send(Request.java:70)
at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12)
at autouploadermodel.VimeoTest.signAndSendToVimeo(VimeoTest.java:282)
at autouploadermodel.VimeoTest.sendVideoBytes(VimeoTest.java:130)
at autouploadermodel.VimeoTest.sendVideo(VimeoTest.java:105)
at autouploadermodel.VimeoTest.main(VimeoTest.java:62)
Caused by: java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:622)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:634)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1317)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at org.scribe.model.Response.<init>(Response.java:28)
at org.scribe.model.Request.doSend(Request.java:110)
at org.scribe.model.Request.send(Request.java:62)
... 5 more
Java Result: 1
最佳答案
我认为您的问题可能只是这一行的结果:
request.addHeader("Content-Range", "bytes%20" + contentRange);
"bytes%20"
通过简单
"bytes "
Headers: {
Content-Length=15125120,
Content-Type=video/mp4,
Content-Range=bytes%200-10485759/15125120 <-- INCORRECT
}
Content-Range
的话题...
14680064-15125119/15125120
的范围。 .这是 HTTP 1.1 规范的一部分。
关于java - 分块上传视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10110479/
我正在使用 WCF 并希望将大文件从客户端上传到服务器。我已经调查并决定遵循 http://msdn.microsoft.com/en-us/library/aa717050.aspx 中概述的分块方
我试图了解有关 Transfer-Encoding:chunked 的更多信息。引用了一些文章: http://zoompf.com/blog/2012/05/too-chunky和 "Transfe
我们正在评估 HDF5 在分块数据集方面的性能。 特别是我们试图弄清楚是否可以跨不同的连续块进行读取以及这样做会如何影响性能? 例如。我们有一个块大小为 10 的数据集,一个有 100 个值的数据集,
使用 Eloquent,如何根据 chunk 中的条件终止分块函数的关闭?我试过返回,但这似乎只终止当前块而不是所有块。此时,我想停止从数据库中检索记录。 $query->chunk(self::CH
有没有办法在不删除所选文件的情况下重新启动 plupload 上传? plupload.stop() 停止上传,但如果我使用 start() 再次启动上传,它会从上次停止的地方继续。相反,我希望它再次
我有带有“id,名称”的文件1和带有“id,地址”的文件2。我无法加载第一个文件(小于 2Gb):它在 76k 行(带有 block 连接)和只有 2 列后崩溃...我也无法在第二个文件上 read_
我正在尝试从头开始设计一个系统,我想在其中通过 servlet 加载文本行。生产线的生产需要一些时间。因此,我希望能够在它们到达时在我的浏览器中逐步显示它们,一次显示几个。我想从 javascript
能否请您提供一个示例,说明如何在 Android 中读取来自 Web 服务的分块响应 谢谢 编辑:我尝试调用一个 soap 网络服务,它用代表图像的 base64 编码字符串回复我 代码如下: Str
我想制作一个无限平铺 map ,从(-max_int,-max_int)到(max_int,max_int),所以我要制作一个基本结构: chunk,每个 chunk 包含 char tiles[w]
这是一个典型的场景:评估一个页面,并且有一个缓冲区 - 一旦缓冲区已满,评估的页面部分就会发送到浏览器。这使用 HTTP 1.1 分块编码。 但是,其中一个 block 中可能会发生错误(在第一个 b
如何从给定模式的句子中获取所有 block 。例子 NP:{} 标记的句子: [("money", "NN"), ("market", "NN") ("fund", "NN")] 如果我解析我得到 (
我正在使用以下代码将 CSV 文件拆分为多个 block (来自 here) def worker(chunk): print len(chunk) def keyfunc(row):
我想我已经很接近这个了,我有以下 dropzone 配置: Dropzone.options.myDZ = { chunking: true, chunkSize: 500000, ret
因为我在更常规的基础上使用 WebSocket 连接,所以我对事情在幕后的工作方式很感兴趣。因此,我研究了无休止的规范文档一段时间,但到目前为止,我真的找不到任何关于对传输流本身进行分 block 。
我有一个 slice ,其中包含约 210 万个日志字符串,我想创建一个 slice ,字符串尽可能均匀分布。 这是我目前所拥有的: // logs is a slice with ~2.1 mill
问题: 我有一个大约为 [350000, 1] 的向量,我希望计算成对距离。这导致 [350000, 350000] 整数数据类型的矩阵不适合 RAM。我最终想得到一个 bool 值(适合 RAM),
我想将 JSONP 用于具有 x 域脚本编写的项目,但不太关心 IE 中的 2048 个字符限制。 如果字符大小超过 2048,JSONP 是否自动支持“分块”?如果是的话,有人可以分享一些例子吗?
我目前正在开发 2d 角色扮演游戏,例如《最终幻想 1-4》。基本上,我的平铺 map 可以加载, Sprite 可以在 map 上自由行走。 如何处理与平铺 map 的碰撞? 我创建了三个独立的图
Treetagger 可以进行词性标记和文本分块,这意味着提取口头和名词性从句,如这个德语示例所示: $ echo 'Das ist ein Test.' | cmd/tagger-chunker-g
我应该从服务器流式传输端点,该端点返回带有传输编码的 json:分块。 我有以下代码,但无法读取响应。我尝试了 responseBody.streamBytes() 并将输入流转换为字符串,但我不能在
我是一名优秀的程序员,十分优秀!