首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Java+Google数据接口测量上传码率

如何使用Java+Google数据接口测量上传码率
EN

Stack Overflow用户
提问于 2011-03-02 12:05:42
回答 1查看 334关注 0票数 2

我正在编写一个Java客户端应用程序,它使用Google Data API将内容上传到youtube。我想知道如何跟踪上传的进度,使用Google Data API库我只是调用service.insert来插入一个新的视频,它会一直阻塞到它完成为止。

有没有其他人想出一个解决方案来监控上传的状态,并在发送时计算字节数?

谢谢你的任何想法

链接:

http://code.google.com/apis/youtube/2.0/developers_guide_java.html#Direct_Upload

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-08 09:55:11

扩展com.google.gdata.data.media.MediaSource writeTo()以包含bytesRead的计数器:

代码语言:javascript
复制
     public static void writeTo(MediaSource source, OutputStream outputStream)
        throws IOException {

      InputStream sourceStream = source.getInputStream();
      BufferedOutputStream bos = new BufferedOutputStream(outputStream);
      BufferedInputStream bis = new BufferedInputStream(sourceStream);
      long byteCounter = 0L; 

      try {
        byte [] buf = new byte[2048]; // Transfer in 2k chunks
        int bytesRead = 0;
        while ((bytesRead = bis.read(buf, 0, buf.length)) >= 0) {
          // byte counter
          byteCounter += bytesRead;
          bos.write(buf, 0, bytesRead); 
        }
        bos.flush();
      } finally {
        bis.close();
      }
    }
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5163266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档