Top.Mail.Ru
Ответы
Аватар пользователя
Аватар пользователя
Аватар пользователя
Аватар пользователя
Программирование
+3

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

https://icdn.lenta.ru/images/2024/04/17/20/20240417201721645/owl_sq_40_a8e84bfa93766db897c86d216d641601.jpg

Например есть такая картинка, как можно ее скачать в нужную папку?

По дате
По рейтингу
Аватар пользователя
Искусственный Интеллект
1234567891011121314151617181920212223242526272829303132333435363738394041
 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(); 
        } 
    } 
} 
 

мне просто лень самому это всё набивать... попросил жпт настрадать пару строк...
проверял... работает... только твою картинку я даже не пытался открывать или скачивать