Issue
I'm making a small application in Java that requires me to scrape an image from a website and display it in a GUI. Now I'm not asking how to get the image's absolute URL, I'm asking how I can display it once I've gotten the absolute URL. I'm using the jsoup library as the web scraper.
Solution
I used the following piece of code to get the desired output shown in the image below (Use appropriate imports):
BufferedImage myPicture = null;
try {
URL url = new URL("https://www.w3schools.com/css/img_fjords.jpg");
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "MyAppName");
myPicture = ImageIO.read(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
frame.getContentPane().add(picLabel);
For setRequestProperty, use any string in place of MyAppName, it's just a value to the User-Agent attribute in the http request made by your app
Answered By - mbhargav294