我想通過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 –