Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用;“发送文件”;电报机器人API中使用Java发送文件的方法_Java_Http_Telegram Bot - Fatal编程技术网

如何使用;“发送文件”;电报机器人API中使用Java发送文件的方法

如何使用;“发送文件”;电报机器人API中使用Java发送文件的方法,java,http,telegram-bot,Java,Http,Telegram Bot,我想通过Telegram Bot API发送文件,但我不知道如何使用Java(发布多部分/表单数据)和提供的Telegram Bot HTTP API方法,sendDocument 这是我的密码: CloseableHttpClient client = HttpClients.createDefault(); HttpPost upload = new HttpPost("https://api.telegram.org/bot"+Main.token+"/se

我想通过Telegram Bot API发送文件,但我不知道如何使用Java(发布多部分/表单数据)和提供的
Telegram Bot HTTP API
方法,
sendDocument

这是我的密码:

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost upload = new HttpPost("https://api.telegram.org/bot"+Main.token+"/sendDocument?chat_id="+id);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();

        File file = new File(path);

        builder.addBinaryBody(
                "document",
                new FileInputStream(file));

        HttpEntity part = builder.build();

        upload.setEntity(part);

        CloseableHttpResponse response = client.execute(upload);
我希望这对你有帮助

private void sendDocUploadingAFile(Long chatId, java.io.File save,String caption) throws TelegramApiException {

    SendDocument sendDocumentRequest = new SendDocument();
    sendDocumentRequest.setChatId(chatId);
    sendDocumentRequest.setNewDocument(save);
    sendDocumentRequest.setCaption(caption);
    sendDocument(sendDocumentRequest);
}
编辑: 本页可帮助任何人开始使用电报机器人api编码


如果您共享部分项目源代码并尝试使用一些库,我们可以提供更好的帮助。例如,虽然此代码可能解决询问者的问题,但最好解释它是如何工作的,以及它与询问者尝试过的代码之间的区别。@dorukayhan这个问题是7个月前提出的。GitHub上有一个很好的FAQ。。。我想没有必要再多解释。但我添加了这个常见问题解答地址。谢谢你的提示。