Как в приложении Java скачать изображение из интернета и поместить в нужную директорию?

Пытался сделать так

URL url = new URL("http://xxxxx.com/img/logo.png");
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream("X:\\Users\\XX\\xxxxxx\\xxxx\\xxxxxx\\logo.png");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();

но получал java.io.IOException: Server returned HTTP response code: 403 for URL: http://xxxxx.com/img/logo.png


Ответы (1 шт):

Автор решения: sporee

Например, во так:

InputStream inputStream = new URL("http://website.com/image.png").openStream();
Files.copy(inputStream, Paths.get("/path/to/image.png"), StandardCopyOption.REPLACE_EXISTING);
inputStream.close();
→ Ссылка