收藏
回答

微信小店的文件事件消息如何获取文件类型

我是在做自研微信小店的客服,现在收到微信小店的客服消息,想知道当收到文件类型的消息时,我如果知道这个文件是什么类型的,例如是txt还是pdf?,因为目前只是提供了一个cos_url可下载地址,因为他是有有效期的,我需要把他保存到自己的oss服务器

回答关注问题邀请回答
收藏

3 个回答

  • 微信小店技术助手-1
    微信小店技术助手-1
    2025-09-09

    当收到文件类型消息(`msg_type`为`file`)时,`cos_url`为临时下载链接,无法直接获取文件类型。需通过下载响应头中的`Content-Type`字段判断文件类型(如`text/plain`、`application/pdf`),并在有效期内完成下载与上传。


    ### 处理步骤:

    1. **解析`Content-Type`**:通过HTTP响应头获取文件类型。

    2. **下载文件**:使用`HttpURLConnection`下载至本地临时目录。

    3. **上传至OSS**:将文件上传至自定义OSS(如阿里云OSS)。

    4. **清理资源**:删除本地临时文件。


    ### Spring Boot 示例代码

    ```java

    @RestController

    @RequestMapping("/wechat/kf")

    public class WeChatKfController {


    @Autowired

    private OSSClient ossClient;


    @PostMapping("/receive")

    public ResponseEntity<String> receiveFileMessage(@RequestBody Map<String, Object> requestBody) {

    String msgType = (String) requestBody.get("msg_type");

    if ("file".equals(msgType)) {

    String cosUrl = (String) ((Map<String, Object>) requestBody.get("file")).get("cos_url");

    try {

    URL url = new URL(cosUrl);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setRequestMethod("GET");

    conn.setConnectTimeout(10000);

    conn.setReadTimeout(10000);


    if (conn.getResponseCode() != 200) return ResponseEntity.status(500).body("下载失败");


    String contentType = conn.getHeaderField("Content-Type");

    String ext = getFileExtension(contentType);

    String fileName = "wechat_file_" + System.currentTimeMillis() + "." + ext;

    File tempFile = File.createTempFile("wechat_", "." + ext);


    try (InputStream in = conn.getInputStream(); FileOutputStream out = new FileOutputStream(tempFile)) {

    byte[] buffer = new byte[1024];

    int len;

    while ((len = in.read(buffer)) > 0) out.write(buffer, 0, len);

    }


    String ossKey = "wechat/files/" + fileName;

    ossClient.putObject("your-bucket", ossKey, tempFile);

    tempFile.deleteOnExit();


    System.out.println("文件已保存至OSS: " + ossKey + ", 类型: " + contentType);

    return ResponseEntity.ok("ok");

    } catch (Exception e) {

    e.printStackTrace();

    return ResponseEntity.status(500).body("处理失败");

    }

    }

    return ResponseEntity.ok("ok");

    }


    private String getFileExtension(String contentType) {

    return switch (contentType) {

    case "text/plain" -> "txt";

    case "application/pdf" -> "pdf";

    case "image/jpeg" -> "jpg";

    case "image/png" -> "png";

    case "application/vnd.ms-excel" -> "xls";

    case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" -> "xlsx";

    default -> "bin";

    };

    }

    }

    ```


    ### 注意事项

    - `cos_url`有效期短,必须立即处理。

    - `Content-Type`是判断文件类型的唯一依据。

    - 需配置OSS客户端AccessKey与Endpoint。


    参考文档:

    [微信小店/商家客服API/接收消息与事件](https://developers.weixin.qq.com/doc/store/shop/API/kf/receive_msg_event/receive_msg_event.html)

    [微信小店/商家客服API/发送消息/文件消息](https://developers.weixin.qq.com/doc/store/shop/API/kf/send_msg/send_file_msg_api.html)

    2025-09-09
    有用
    回复
  • 智能回答 智能回答 本次回答由AI生成
    2025-09-08
    有用 1
  • Forever
    Forever
    2025-09-08

    有没有案列,我这边用的技术是Springboot,能否提供下解析实例代码

    2025-09-08
    有用
    回复
登录 后发表内容