Skip to content

[开发帮助] 下载文件与图片消息接口 #73

@h1634668150

Description

@h1634668150

问题描述

这个是我的代码

void forwardFileMessage(Object msgInfoBean, String sourceName, List targetWxids, String currentTime, String content) {
    try {
        String filePath = invokeStringMethod(msgInfoBean, "getFilePath");
        String fileName = extractXmlValue(content, "title");
        log("原始文件名: " + content); 
        if (TextUtils.isEmpty(filePath)) {
            // 尝试从消息中提取cdnattachurl,然后下载文件
            String cdnUrl = extractXmlValue(content, "cdnattachurl");
            if (!TextUtils.isEmpty(cdnUrl)) {
                // 生成临时文件路径
                String tempDir = cacheDir + "/files/";
                File tempFolder = new File(tempDir);
                if (!tempFolder.exists()) {
                    tempFolder.mkdirs();
                }
                String fileExt = extractXmlValue(content, "fileext");
                final String tempFilePath = tempDir + System.currentTimeMillis() + "." + (TextUtils.isEmpty(fileExt) ? "bin" : fileExt);
                final String finalFileName = fileName;
                final List finalTargetWxids = targetWxids;
                final String finalSourceName = sourceName;
                final String finalCurrentTime = currentTime;
                
                // 构建微信文件下载URL
                String downloadUrl = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia?msgid=" + cdnUrl + "&type=slave";
                log("尝试下载文件: " + downloadUrl + " -> " + tempFilePath);
                
                // 创建下载回调对象
                download(downloadUrl, tempFilePath, null, new PluginCallBack.DownloadCallback() {
                    public void onSuccess(File file) {
                        String path = file.getAbsolutePath();
                        log("文件下载成功: " + path);
                        // 下载成功后转发文件
                        try {
                            String textContent = formatForwardContent(finalSourceName, "[文件] " + (TextUtils.isEmpty(finalFileName) ? file.getName() : finalFileName), finalCurrentTime);
                            for (int j = 0; j < finalTargetWxids.size(); j++) {
                                String targetWxid = (String) finalTargetWxids.get(j);
                                try {
                                    sendText(targetWxid, textContent);
                                    Thread.sleep(100);
                                    // 使用shareFile方法发送文件,appId传入空字符串
                                    shareFile(targetWxid, TextUtils.isEmpty(finalFileName) ? file.getName() : finalFileName, path, "");
                                    log("文件发送成功: " + path);
                                } catch (Exception e) {
                                    log("转发文件失败: " + e.getMessage());
                                    // 如果发送失败,尝试只发送文本消息
                                    sendText(targetWxid, textContent);
                                }
                            }
                        } catch (Exception e) {
                            log("下载后转发文件异常: " + e.getMessage());
                        }
                    }
                    
                    public void onError(Exception e) {
                        log("文件下载失败: " + e.getMessage());
                        // 下载失败后只发送文本消息
                        try {
                            String textContent = formatForwardContent(finalSourceName, "[文件] " + (TextUtils.isEmpty(finalFileName) ? "未知文件" : finalFileName), finalCurrentTime);
                            for (int j = 0; j < finalTargetWxids.size(); j++) {
                                String targetWxid = (String) finalTargetWxids.get(j);
                                sendText(targetWxid, textContent);
                                Thread.sleep(100);
                            }
                        } catch (Exception ex) {
                            log("下载失败后发送文本异常: " + ex.getMessage());
                        }
                    }
                });
                
                // 由于是异步下载,直接返回
                return;
            }
        }
        
        if (TextUtils.isEmpty(filePath)) {
            // 如果仍然无法获取文件路径,只发送文本消息
            String textContent = formatForwardContent(sourceName, "[文件] " + (TextUtils.isEmpty(fileName) ? "未知文件" : fileName), currentTime);
            for (int j = 0; j < targetWxids.size(); j++) {
                String targetWxid = (String) targetWxids.get(j);
                sendText(targetWxid, textContent);
                Thread.sleep(100);
            }
            return;
        }
        
        log("获取到文件路径: " + filePath);
        String textContent = formatForwardContent(sourceName, "[文件] " + (TextUtils.isEmpty(fileName) ? new File(filePath).getName() : fileName), currentTime);
        
        for (int j = 0; j < targetWxids.size(); j++) {
            String targetWxid = (String) targetWxids.get(j);
            try {
                sendText(targetWxid, textContent);
                Thread.sleep(100);
                // 使用shareFile方法发送文件,appId传入空字符串
                log("尝试发送文件: " + filePath + " 到 " + targetWxid);
                shareFile(targetWxid, TextUtils.isEmpty(fileName) ? new File(filePath).getName() : fileName, filePath, "");
                log("文件发送成功: " + filePath);
            } catch (Exception e) {
                log("转发文件失败: " + e.getMessage());
                // 如果发送失败,尝试只发送文本消息
                sendText(targetWxid, textContent);
            }
        }
    } catch (Exception e) {
        log("转发文件消息异常: " + e.getMessage());
    }
}

这个是文件的内容

<msg><appmsg appid="wx6618f1cfc6c132f8"><title>**.docx</title><type>6</type><appattach><totallen>11225</totallen><attachid>@cdn_3057020100044b304902010002042f45fa6b02032f80290204def73db7020469aa355f042433306165636632632d313236372d346136662d613464392d6434623235366263623333620204012800050201000405004c54a100_f1d2a44153e798bef4201c6c664fbb3f_1</attachid><cdnattachurl>3057020100044b304902010002042f45fa6b02032f80290204def73db7020469aa355f042433306165636632632d313236372d346136662d613464392d6434623235366263623333620204012800050201000405004c54a100</cdnattachurl><aeskey>f1d2a44153e798bef4201c6c664fbb3f</aeskey><fileext>docx</fileext><overwrite_newmsgid>1527280791365552312</overwrite_newmsgid><fileuploadtoken><![CDATA[v1_i2iPTOOaxirmE4mB4H18s3KJfQ/uyhxGHE63j4UVBYG3ahnYZKqCHRY0aeVjJ8hIuRFVDEKt3kBmgHrObQoQuV9K5FLmtWTO4vEl/4xD7hdbQyirCr4pUc6gB3ocotnVx3OmD+yJRkL3/Mh7nnm5E5DfvFwn3tIY9BJRLfvHayztWIBuIU7xzPyI2GV0INnBvYErbJP1fkiuEE9ntqtBugchE4xBl6vTJ6lCnwIoB7kqTVAzoB2FtnUCk2jbY9djaH0Tvi08u4s2IRltDxCztY8Xf0/C/mAiwPW2RGdO]]></fileuploadtoken></appattach><md5>cf93f39d8159b87089438df482544fc9</md5></appmsg><fromusername>wxid_7fwz7hj7j4wc21</fromusername><appinfo><version>7</version><appname>微信电脑版</appname></appinfo><extcommoninfo><media_expire_at>1773972179</media_expire_at></extcommoninfo></msg>

其他要求

  • 我在 Issue 没有发现相同问题
  • 我已知晓不按照模板提供的指示进行填写, 则我的 Issue 可能会被无条件删除

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions