Java 按块上传视频文件
是的,这是一个很长的问题,有很多细节…所以,我的问题是:如何将上传流分段传输到Vimeo 对于希望在自己的机器上复制和调试的任何人:以下是您需要的东西:Java 按块上传视频文件,java,api,upload,vimeo,scribe,Java,Api,Upload,Vimeo,Scribe,是的,这是一个很长的问题,有很多细节…所以,我的问题是:如何将上传流分段传输到Vimeo 对于希望在自己的机器上复制和调试的任何人:以下是您需要的东西: 我的密码 包括找到的Scribe库 拥有至少大于10 MB的有效视频文件(mp4),并将其放入目录C:\test.mp4,或将该代码更改为指向您所在的位置 就这样!谢谢你帮助我 大更新:我在代码中为Vimeo留下了一个可用的API密钥和秘密。因此,只要您有一个Vimeo帐户,一旦您允许应用程序并输入您的令牌,所有代码都应该可以正常工作。只需
- 我的密码
- 包括找到的Scribe库
- 拥有至少大于10 MB的有效视频文件(mp4),并将其放入目录
,或将该代码更改为指向您所在的位置C:\test.mp4
- 就这样!谢谢你帮助我李>
- 不向第一个区块添加内容范围标头
- 将前缀添加到内容范围标题(每个标题都包含前一个标题的组合):
- “字节”
- “bytes”(抛出连接错误,请查看最下面的错误)->在中显示这就是他们要查找的内容,但我很确定文档中有拼写错误,因为他们在“resume”示例中的内容范围标题为:
,而它应该是1001-339108/339108
。所以嗯1001-339107/339108
- “字节%20”
- “字节:”
- “字节:”
- “字节=”
- “字节=”
- 不将任何内容作为前缀添加到内容范围标头
/**
* 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
}
这里是printRequest和printResponse方法的部分输出(例如……可以找到所有输出):注意此输出根据contentRangeHeaderPrefix
设置为什么以及first
布尔值设置为什么而变化(它指定是否在第一个块上包含内容范围标头)
然后代码继续完成上传和设置视频信息(您可以在中看到)
编辑2:尝试从内容范围中删除“%20”,但在建立连接时收到此错误。我必须使用“字节%20”或根本不添加“字节”
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
线程“main”org.scribe.exceptions.oautheexception中的异常:创建连接时出现问题。
位于org.scribe.model.Request.send(Request.java:70)
在org.scribe.model.OAuthRequest.send上(OAuthRequest.java:12)
在autouploadermodel.VimeoTest.signAndSendToVimeo(VimeoTest.java:282)中
在autouploadermodel.VimeoTest.sendVideoBytes(VimeoTest.java:130)上
在autouploadermodel.VimeoTest.sendVideo(VimeoTest.java:105)上
在autouploadermodel.VimeoTest.main(VimeoTest.java:62)上
原因:java.io.IOException:写入服务器时出错
位于sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:622)
位于sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:634)
位于sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1317)
位于java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
位于org.scribe.model.Response(Response.java:28)
位于org.scribe.model.Request.doSend(Request.java:110)
位于org.scribe.model.Request.send(Request.java:62)
…还有5个
Java结果:1
编辑1:更新了代码和输出。仍然需要帮助!此处
String contentRange = Integer.toString(byteNumber + 1);
在第一次迭代中,从1开始,而不是从0开始
这里
您输入的是整个文件内容的长度,而不是当前块的长度。我认为您的问题可能只是这一行的结果:
request.addHeader("Content-Range", "bytes%20" + contentRange);
尝试用简单的“bytes”
替换“bytes”
在输出中可以看到相应的h
String contentRange = Integer.toString(byteNumber + 1);
request.addHeader("Content-Length", contentLength);
request.addHeader("Content-Range", "bytes%20" + contentRange);
Headers: {
Content-Length=15125120,
Content-Type=video/mp4,
Content-Range=bytes%200-10485759/15125120 <-- INCORRECT
}
request = new OAuthRequest(Verb.PUT, "http://vimeo.com/api/rest/v2");
request.addQuerystringParameter("method", "vimeo.videos.upload.complete");
request.addQuerystringParameter("filename", video.getName());
request.addQuerystringParameter("ticket_id", ticket);
service.signRequest(token, request);
response = request.send();
String contentRange="bytes "+lastBytesSend+"-"+ ((totalSize - lastBytesSend)-1)+"/"+totalSize ;
request.addHeader("Content-Range",contentRange);