What a problem may be? Full code for telegram bot for user upload file to the local disk.
private File downloadFileFromTelegram(String fileName, @NonNull String fileId) throws TelegramApiException { GetFile getFile = new GetFile();
getFile.setFileId(fileId);
org.telegram.telegrambots.meta.api.objects.File file = execute(getFile);
// Use the system's temporary directory
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
Path filePath = tempDir.resolve(fileName);
// Use try-with-resources for the URLConnection and InputStream
try (InputStream inputStream = new BufferedInputStream(new URL(file.getFileUrl(getBotToken())).openStream());
FileOutputStream outputStream = new FileOutputStream(filePath.toFile())) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return filePath.toFile();
} catch (IOException e) {
e.printStackTrace();
// Handle the exception as needed
return null;
}
}
Is it another methods for this?