问题遇到的现象和发生背景
企业微信分片拉取多媒体数据的时候拉取第二个512k的内容时会拉取失败?
问题相关代码,请勿粘贴截图
// 拉取媒体信息
private String pullMediaFiles(long sdk, String msgtype, JSONObject plaintextJson) throws Exception {
String[] msgtypeStr = {MsgType.IMAGE.getCode(), MsgType.VOICE.getCode(), MsgType.VIDEO.getCode(), MsgType.EMOTION.getCode(), MsgType.FILE.getCode()};
List<String> msgtypeList = Arrays.asList(msgtypeStr);
if (msgtypeList.contains(msgtype)) {
String savefileName = "";
JSONObject file = new JSONObject();
String msgid = plaintextJson.getString("msgid");
if (msgid != null) {
file = plaintextJson.getJSONObject(msgtype);
savefileName = plaintextJson.getString("msgid");
} else {
// 混合消息
file = plaintextJson;
savefileName = file.getString("md5sum");
}
/* ============ 文件存储目录及文件名 Start ============ */
String suffix = "";
switch (msgtype) {
case "image":
suffix = ".jpg";
break;
case "voice":
suffix = ".amr";
break;
case "video":
suffix = ".mp4";
break;
case "emotion":
int type = (int) file.get("type");
if (type == 1) suffix = ".gif";
else if (type == 2) suffix = ".png";
break;
case "file":
suffix = "." + file.getString("fileext");
break;
}
savefileName += suffix;
/* ============ 文件存储目录及文件名 End ============ */
/* ============ 拉去文件 Start ============ */
String savefile = "/var/data/workwx/" + savefileName;
int i = 0;
boolean isSave = true;
String indexbuf = "", sdkfileid = file.getString("sdkfileid");
File tempFile = new File(savefile);
String path = null;
while (true) {
long mediaData = Finance.NewMediaData();
int ret = Finance.GetMediaData(sdk, indexbuf, sdkfileid, "", "", timeout, mediaData);
if (ret != 0) {
System.out.println("getmediadata ret:" + ret);
Finance.FreeMediaData(mediaData);
System.out.println("=========================" + path + suffix);
return path;
}
System.out.printf("getmediadata outindex len:%d, data_len:%d, is_finis:%d\n",
Finance.GetIndexLen(mediaData), Finance.GetDataLen(mediaData),
Finance.IsMediaDataFinish(mediaData));
try {
// 大于512k的文件会分片拉取,此处需要使用追加写,避免后面的分片覆盖之前的数据。
FileOutputStream output = new FileOutputStream(tempFile, true);
output.write(Finance.GetData(mediaData));
//获取或创建container
output.close();
} catch (Exception e) {
e.printStackTrace();
}
if (Finance.IsMediaDataFinish(mediaData) == 1) {
// 已经拉取完成最后一个分片
Finance.FreeMediaData(mediaData);
break;
} else {
// 获取下次拉取需要使用的indexbuf
indexbuf = Finance.GetOutIndexBuf(mediaData);
Finance.FreeMediaData(mediaData);
}
// 若文件大于50M则不保存
if (++i > 100) {
isSave = false;
break;
}
}
if (isSave) {
path = BlobHelper.saveFileToBlogStorage(tempFile, containerName, storageConfig);
}
/* ============ 拉去文件 End ============ */
return path;
}
return null;
}
运行结果及报错内容
sdk:1975169874416
ret:10000
我的解答思路和尝试过的方法
和官方文档一样 但是依然报错
我想要达到的结果
ret:0