我正在编写一个Java客户端应用程序,它使用Google Data API将内容上传到youtube。我想知道如何跟踪上传的进度,使用Google Data API库我只是调用service.insert来插入一个新的视频,它会一直阻塞到它完成为止。
有没有其他人想出一个解决方案来监控上传的状态,并在发送时计算字节数?
谢谢你的任何想法
链接:
http://code.google.com/apis/youtube/2.0/developers_guide_java.html#Direct_Upload
发布于 2011-03-08 09:55:11
扩展com.google.gdata.data.media.MediaSource writeTo()以包含bytesRead的计数器:
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();
}
}
}https://stackoverflow.com/questions/5163266
复制相似问题