GetFileFromUrlUtil.java 762 Bytes
package com.infoloop.tianting.utils;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class GetFileFromUrlUtil {
    public static InputStream getInputStreamFromUrl(final String url) throws IOException {
        HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
        httpUrl.connect();
        int responseCode = httpUrl.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            log.error("Failed to download file:{}, Response code:{} ", url, responseCode);
            throw new IOException("Failed to download file:" + url);
        }
        return httpUrl.getInputStream();
    }
}