import com.hjk.util.FileUtil;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
public class ImageTest {
public static void main(String[] args) throws IOException {
//百度随便找的图片,可以替换成自己需要使用的图片
String imageUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg9.51tietu.net%2Fpic%2F2019-091114%2Fgw31fov5r4igw31fov5r4i.jpg&refer=http%3A%2F%2Fimg9.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653186047&t=d9b5d5f17e4c2e4a3bb0f256851a5b5e";
InputStream inputStream = FileUtil.getInputStreamByUrl(imageUrl);
BufferedImage bufferedImage = ImageIO.read(inputStream);
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
}
}
import java.net.HttpURLConnection;
import java.io.ByteArrayOutputStream;
import org.apache.commons.io.IOUtils;
/**
* 文件上传下载的工具类
*/
public abstract class FileUtil {
public static InputStream getInputStreamByUrl(String strUrl) {
HttpURLConnection conn = null;
try {
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20 * 1000);
conn.setRequestProperty("Charset", "UTF-8");
final ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(conn.getInputStream(), output);
return new ByteArrayInputStream(output.toByteArray());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}