java download image file

https:/‮/‬www.lautturi.com
java download image file

To download an image file from a URL in Java, you can use the URL and URLConnection classes from the java.net package to open a connection to the file, and the InputStream and OutputStream classes from the java.io package to read and write the file.

Here's an example of how to download an image file from a URL in Java:

import java.io.*;
import java.net.*;

public class ImageDownloader {
    public static void main(String[] args) throws IOException {
        // Define the URL of the image to download
        String imageUrl = "https://example.com/images/myimage.jpg";

        // Open a connection to the URL
        URL url = new URL(imageUrl);
        URLConnection connection = url.openConnection();

        // Set the user agent to pretend to be a web browser
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

        // Connect to the URL
        connection.connect();

        // Get the input stream to read the image
        InputStream inputStream = connection.getInputStream();

        // Create a file output stream to save the image
        FileOutputStream outputStream = new FileOutputStream("myimage.jpg");
Created Time:2017-11-03 15:57:11  Author:lautturi