All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.afu.utils.http.Downloader Maven / Gradle / Ivy

There is a newer version: 0.0.55-RELEASE
Show newest version
package io.afu.utils.http;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class Downloader {

    public static void d(String url,String savePath) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response = httpClient.execute(httpGet);
            try {
                httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
                HttpEntity entity = response.getEntity();
                long len = entity.getContentLength();
                InputStream inputStream = entity.getContent();
                FileOutputStream fileOutputStream = new FileOutputStream(new File(savePath));
                int inByte;
                while ((inByte = inputStream.read()) != -1){
                    fileOutputStream.write(inByte);
                }
                inputStream.close();
                fileOutputStream.close();
            }finally {
                response.close();
            }
        }finally {
            httpClient.close();
        }
    }






}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy