package com.gic.haoban.manage.service.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

/**
 * Created by wangzubo on 2023/3/20.
 */
public class StreamUtils {

    private static Logger logger = LogManager.getLogger(StreamUtils.class);

    /**
     * 根据图片url地址获取其流
     * @param url 网络图片地址
     * @return 图片流
     */
    public static InputStream getImageStream(String url) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setReadTimeout(10000);
            connection.setConnectTimeout(10000);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                return inputStream;
            }
        } catch (IOException e) {
            logger.error("获取网络图片出现异常，图片路径为：" + url,e);
        }
        return null;
    }

}
