Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按块上传视频文件_Java_Api_Upload_Vimeo_Scribe - Fatal编程技术网

Java 按块上传视频文件

Java 按块上传视频文件,java,api,upload,vimeo,scribe,Java,Api,Upload,Vimeo,Scribe,是的,这是一个很长的问题,有很多细节…所以,我的问题是:如何将上传流分段传输到Vimeo 对于希望在自己的机器上复制和调试的任何人:以下是您需要的东西: 我的密码 包括找到的Scribe库 拥有至少大于10 MB的有效视频文件(mp4),并将其放入目录C:\test.mp4,或将该代码更改为指向您所在的位置 就这样!谢谢你帮助我 大更新:我在代码中为Vimeo留下了一个可用的API密钥和秘密。因此,只要您有一个Vimeo帐户,一旦您允许应用程序并输入您的令牌,所有代码都应该可以正常工作。只需

是的,这是一个很长的问题,有很多细节…所以,我的问题是:如何将上传流分段传输到Vimeo

对于希望在自己的机器上复制和调试的任何人:以下是您需要的东西:

  • 我的密码
  • 包括找到的Scribe库
  • 拥有至少大于10 MB的有效视频文件(mp4),并将其放入目录
    C:\test.mp4
    ,或将该代码更改为指向您所在的位置
  • 就这样!谢谢你帮助我
大更新:我在代码中为Vimeo留下了一个可用的API密钥和秘密。因此,只要您有一个Vimeo帐户,一旦您允许应用程序并输入您的令牌,所有代码都应该可以正常工作。只需将该链接中的代码复制到您最喜欢的IDE上的项目中,然后看看是否可以与我一起解决这个问题。我会把赏金给任何给我工作密码的人。谢谢哦,别指望长时间使用这把钥匙和这个秘密。此问题解决后,我将删除它。:)

问题概述:问题在于,当我将最后一个字节块发送到Vimeo,然后验证上传时,响应返回所有内容的长度仅为最后一个字节块的长度,而不是所有的字节块的组合长度

SSCCE注意:我有我的整个SSCCE。我把它放在别的地方,这样它就可以C兼容了。它不是很短(大约300行),但希望您发现它包含了Self,并且它肯定是一个E示例!)。然而,我在这篇文章中发布了我代码的相关部分

这就是它的工作原理:当您通过流媒体方法将视频上传到Vimeo时(请参阅upload API documentation for setup以了解这一点),您必须给出几个标题:端点、内容长度和内容类型。文档中说它忽略了任何其他标题。您还可以为它提供正在上载的文件的字节信息负载。然后签名并发送(我有一个方法可以使用)

我的问题:当我在一个请求中发送视频时,一切都很好。我的问题是,当我上传几个更大的文件时,我使用的计算机没有足够的内存来加载所有字节信息并将其放入HTTP put请求,因此我必须将其拆分为1 MB的段。这就是事情变得棘手的地方。文档中提到可以“恢复”上传,所以我尝试用我的代码来实现这一点,但它并不完全正常。下面,您将看到发送视频的代码记住我的SSCCE是

我尝试过的事情:我认为这与内容范围标题有关。。。下面是我在更改内容范围标题时尝试过的内容

  • 不向第一个区块添加内容范围标头
  • 将前缀添加到内容范围标题(每个标题都包含前一个标题的组合):

    • “字节”
    • “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);