我想通过Telegram Bot API发送文件,但我不知道我应该如何在Java中执行此操作(发布multipart/form-数据)提供Telegram Bot HTTP API
方法,sendDocument
。如何在Telegram Bot API中使用“sendDocument”方法使用Java发送文件
这里是我的代码:
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);
我们可以帮助更好,如果你共享的项目源代码的一部分 –
尝试使用一些库,例如https://github.com/pengrad/java-telegram-bot-api –