src/main/java/io/github/biezhi/wechat/api/WeChatApiImpl.java
@Override
    public MediaResponse uploadMedia(String toUser, String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new WeChatException("文件[" + filePath + "]不存在");
        }
        log.info("开始上传文件: {}", filePath);
        long   size      = file.length();
        String mimeType  = WeChatUtils.getMimeType(filePath);
        String mediatype = "doc";
        if (mediatype.contains("image")) {
            mediatype = "pic";
        }
        if (mediatype.contains("audio")) {
            mediatype = "audio";
        }
        if (mediatype.contains("video")) {
            mediatype = "video";
        }
        String url     = String.format("%s/webwxuploadmedia?f=json", bot.session().getFileUrl());
        String mediaId = System.currentTimeMillis() / 1000 + StringUtils.random(6);

        Map<String, Object> uploadMediaRequest = new HashMap<>(10);
        uploadMediaRequest.put("UploadType", 2);
        uploadMediaRequest.put("BaseRequest", bot.session().getBaseRequest());
        uploadMediaRequest.put("ClientMediaId", mediaId);
        uploadMediaRequest.put("TotalLen", size);
        uploadMediaRequest.put("StartPos", 0);
        uploadMediaRequest.put("DataLen", size);
        uploadMediaRequest.put("MediaType", 4);
        uploadMediaRequest.put("FromUserName", bot.session().getUserName());
        uploadMediaRequest.put("ToUserName", toUser);
        uploadMediaRequest.put("FileMd5", MD5Checksum.getMD5Checksum(file.getPath()));

        String dataTicket = this.client.cookie("webwx_data_ticket");
        if (StringUtils.isEmpty(dataTicket)) {
            throw new WeChatException("缺少了附件Cookie");
        }

        ApiResponse response = this.client.send(new StringRequest(url).post().multipart()
                .fileName(file.getName())
                .add("id", "WU_FILE_0")
                .add("name", filePath)
                .add("type", mimeType)
                .add("lastModifieDate", new SimpleDateFormat("yyyy MM dd HH:mm:ss").format(new Date()))
                .add("size", String.valueOf(size))
                .add("mediatype", mediatype)
                .add("uploadmediarequest", WeChatUtils.toJson(uploadMediaRequest))
                .add("webwx_data_ticket", dataTicket)
                .add("pass_ticket", bot.session().getPassTicket())
                .add("filename", RequestBody.create(MediaType.parse(mimeType), file)));

        MediaResponse mediaResponse = response.parse(MediaResponse.class);
        if (!mediaResponse.success()) {
            log.warn("上传附件失败: {}", mediaResponse.getMsg());
        }
        log.info("文件上传成功: {}", filePath);
        return mediaResponse;
    }
@Override
    public MediaResponse uploadMedia(String toUser, String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new WeChatException("文件[" + filePath + "]不存在");
        }
        log.info("开始上传文件: {}", filePath);
        long   size      = file.length();
        String mimeType  = WeChatUtils.getMimeType(filePath);
        String mediatype = "doc";
        if (mediatype.contains("image")) {
            mediatype = "pic";
        }
        if (mediatype.contains("audio")) {
            mediatype = "audio";
        }
        if (mediatype.contains("video")) {
            mediatype = "video";
        }
        String url     = String.format("%s/webwxuploadmedia?f=json", bot.session().getFileUrl());
        String mediaId = System.currentTimeMillis() / 1000 + StringUtils.random(6);

        Map<String, Object> uploadMediaRequest = new HashMap<>(10);
        uploadMediaRequest.put("UploadType", 2);
        uploadMediaRequest.put("BaseRequest", bot.session().getBaseRequest());
        uploadMediaRequest.put("ClientMediaId", mediaId);
        uploadMediaRequest.put("TotalLen", size);
        uploadMediaRequest.put("StartPos", 0);
        uploadMediaRequest.put("DataLen", size);
        uploadMediaRequest.put("MediaType", 4);
        uploadMediaRequest.put("FromUserName", bot.session().getUserName());
        uploadMediaRequest.put("ToUserName", toUser);
        uploadMediaRequest.put("FileMd5", MD5Checksum.getMD5Checksum(file.getPath()));

        String dataTicket = this.client.cookie("webwx_data_ticket");
        if (StringUtils.isEmpty(dataTicket)) {
            throw new WeChatException("缺少了附件Cookie");
        }

        ApiResponse response = this.client.send(new StringRequest(url).post().multipart()
                .fileName(file.getName())
                .add("id", "WU_FILE_0")
                .add("name", filePath)
                .add("type", mimeType)
                .add("lastModifieDate", new SimpleDateFormat("yyyy MM dd HH:mm:ss").format(new Date()))
                .add("size", String.valueOf(size))
                .add("mediatype", mediatype)
                .add("uploadmediarequest", WeChatUtils.toJson(uploadMediaRequest))
                .add("webwx_data_ticket", dataTicket)
                .add("pass_ticket", bot.session().getPassTicket())
                .add("filename", RequestBody.create(MediaType.parse(mimeType), file)));

        MediaResponse mediaResponse = response.parse(MediaResponse.class);
        if (!mediaResponse.success()) {
            log.warn("上传附件失败: {}", mediaResponse.getMsg());
        }
        log.info("文件上传成功: {}", filePath);
        return mediaResponse;
    }
上传附件

@param toUser   发送给谁
@param filePath 文件路径
@return MediaResponse