简体   繁体   English

如何在电报机器人(JAVA)中下载文件

[英]How to download file in telegram bot (JAVA)

I want to download a file in my telegram bot code many tutorials say that I must use the getFile method that I can't find that in 4.2 version of the telegram API so how I can download a file to a specific destination in host pc?我想在我的电报机器人代码中下载一个文件,许多教程都说我必须使用 getFile 方法,而我在 4.2 版本的电报 API 中找不到该方法,那么如何将文件下载到主机 PC 中的特定目的地? thanks谢谢

Assuming you are using TelegramBot SDK from rubenlagus ( https://github.com/rubenlagus/TelegramBots ), as I faced same issue.假设您使用的是 rubenlagus ( https://github.com/rubenlagus/TelegramBots ) 的 TelegramBot SDK,因为我遇到了同样的问题。 Below is my solution.下面是我的解决方案。

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

I had the same problem.我有同样的问题。 This was my solution.这是我的解决方案。 Not very nice but it works.不是很好,但它有效。

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();
        }


    }

Here I got this solution enter link description here在这里我得到了这个解决方案在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM