Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Как скачать картинку по ссылке через java

Артем Колегов Ученик (175), открыт 2 недели назад
https://icdn.lenta.ru/images/2024/04/17/20/20240417201721645/owl_sq_40_a8e84bfa93766db897c86d216d641601.jpg

Например есть такая картинка, как можно ее скачать в нужную папку?
1 ответ
Александр Искусственный Интеллект (292099) 2 недели назад
 import java.io.*; 
import java.net.URL;

public class ImageDownloader {
public static void main(String[] args) {
String imageUrl = "твоя ссылка"; // URL of the image
String destinationFolder = "/path/to/destination/folder/"; // Destination folder on disk

try {
// Create URL object
URL url = new URL(imageUrl);

// Open a connection to the URL
InputStream inputStream = url.openStream();

// Create destination file
String[] urlParts = imageUrl.split("/");
String fileName = urlParts[urlParts.length - 1];
File destinationFile = new File(destinationFolder + fileName);

// Create FileOutputStream to write to the destination file
OutputStream outputStream = new FileOutputStream(destinationFile);

// Read from the input stream and write to the output stream
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

// Close streams
inputStream.close();
outputStream.close();

System.out.println("Image downloaded successfully to: " + destinationFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
мне просто лень самому это всё набивать... попросил жпт настрадать пару строк...
проверял... работает... только твою картинку я даже не пытался открывать или скачивать
Похожие вопросы