Java use httpurlconnection to download file from an http url

https‮gi.www//:‬iftidea.com

To download a file from an HTTP URL using HttpURLConnection in Java, you can use the following code:

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpDownload {
    public static void main(String[] args) {
        String fileURL = "http://example.com/file.pdf";
        String saveDir = "C:/download";
        try {
            downloadFile(fileURL, saveDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downloadFile(String fileURL, String saveDir) throws IOException {
        URL url = new URL(fileURL);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        int responseCode = httpConn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            String fileName = "";
            String disposition = httpConn.getHeaderField("Content-Disposition");
            String contentType = httpConn.getContentType();
            int contentLength = httpConn.getContentLength();
            if (disposition != null) {
                int index = disposition.indexOf("filename=");
                if (index > 0) {
                    fileName = disposition.substring(index + 10, disposition.length() - 1);
                }
            } else {
                fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1);
            }
            System.out.println("Content-Type = " + contentType);
            System.out.println("Content-Disposition = " + disposition);
            System.out.println("Content-Length = " + contentLength);
            System.out.println("File name = " + fileName);
            InputStream inputStream = httpConn.getInputStream();
            String saveFilePath = saveDir + "/" + fileName;
            FileOutputStream outputStream = new FileOutputStream(saveFilePath);
            int bytesRead = -1;
            byte[] buffer = new byte[4096];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
            inputStream.close();
            System.out.println("File downloaded");
        } else {
            System.out.println("No file to download. Server replied HTTP code: " + responseCode);
        }
        httpConn.disconnect();
    }
}

In this code, the downloadFile() method takes the file URL and the directory where the file should be saved as arguments. It then establishes an HttpURLConnection with the URL, and checks the response code to ensure that the connection is successful. The code then retrieves the file name, content type, content length, and content disposition from the HTTP header. Finally, it reads the file data from the input stream, and writes it to the output stream. After the file has been downloaded, both the input and output streams are closed, and the connection to the URL is disconnected.