【问题标题】:How to download file in telegram bot (JAVA)如何在电报机器人(JAVA)中下载文件
【发布时间】:2020-03-04 10:33:16
【问题描述】:

我想在我的电报机器人代码中下载一个文件许多教程说我必须使用我在 4.2 版本的电报 API 中找不到的 getFile 方法 那么如何将文件下载到主机 PC 中的特定目标? 谢谢

【问题讨论】:

  • 您希望机器人将文件下载到服务器,还是希望机器人强制任何客户端将文件下载到客户端的系统?后者是不可能的(希望!!!)。我认为下载必须由用户发起。这很好。
  • 如果有帮助,您可以检查一下:How can i get file_path of telegram bot

标签: java api file download telegram


【解决方案1】:

假设您使用的是 rubenlagus (https://github.com/rubenlagus/TelegramBots) 的 TelegramBot SDK,因为我遇到了同样的问题。以下是我的解决方案。

GetFile getFile = new GetFile().setFileId(fileId);
String filePath = execute(getFile).getFilePath();
File file = downloadFile(filePath, outputFile);

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。 这是我的解决方案。不是很好,但很有效。

    if (update.getMessage().hasDocument()){
            String doc_id = update.getMessage().getDocument().getFileId();
            String doc_name = update.getMessage().getDocument().getFileName();
            String doc_mine = update.getMessage().getDocument().getMimeType();
            int doc_size = update.getMessage().getDocument().getFileSize();
            String getID = String.valueOf(update.getMessage().getFrom().getId());
    
            Document document = new Document();
            document.setMimeType(doc_mine);
            document.setFileName(doc_name);
            document.setFileSize(doc_size);
            document.setFileId(doc_id);
    
            GetFile getFile = new GetFile();
            getFile.setFileId(document.getFileId());
            try {
                org.telegram.telegrambots.meta.api.objects.File file = execute(getFile);
               downloadFile(file, new File("./data/userDoc/"+getID+"_"+doc_name));
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
    
    
        }
    

    我得到了这个解决方案 enter link description here

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2017-08-18
      • 1970-01-01
      • 2019-06-02
      • 2020-07-18
      • 2017-06-16
      • 2018-03-07
      • 2019-09-16
      • 2020-12-05
      相关资源
      最近更新 更多