使用Java将视频上传到Youtube

时间:2015-10-27 15:57:24

标签: java youtube youtube-api youtube-data-api

我正在尝试使用YouTube API将视频上传到YouTube。使用未更改的代码运行测试时,抛出以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot call getProgress() if the specified AbstractInputStreamContent has no content length. Use  getNumBytesUploaded() to denote progress instead.
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:92)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:49)
at com.google.api.client.googleapis.media.MediaHttpUploader.getProgress(MediaHttpUploader.java:934)
at com.google.api.services.samples.youtube.cmdline.UploadVideo$1.progressChanged(UploadVideo.java:151)
at com.google.api.client.googleapis.media.MediaHttpUploader.updateStateAndNotifyListener(MediaHttpUploader.java:907)
at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:471)
at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:336)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:427)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.google.api.services.samples.youtube.cmdline.UploadVideo.main(UploadVideo.java:165)

我知道为什么以及如何解决它?

2 个答案:

答案 0 :(得分:1)

getProgress()替换为getNumBytesUploaded()

  System.out.println("Upload percentage: " + uploader.getNumBytesUploaded());

它可以上传大型视频。

答案 1 :(得分:0)

要使用getProgress()方法取得进展,API必须知道您尝试上传的文件的大小,您可以使用inputStreamContent.setLength(file.getSize())进行设置。

或者您可以按照建议使用getNumBytesUploaded()