1
我正在Java中製作一個小型應用程序,需要我從網站上刮取圖像並將其顯示在GUI中。現在我不問如何獲取圖像的絕對URL,我問我如何獲得絕對URL後再顯示它。我使用jsoup庫作爲網頁抓取工具。顯示從jsoup網站抓取的圖像
我正在Java中製作一個小型應用程序,需要我從網站上刮取圖像並將其顯示在GUI中。現在我不問如何獲取圖像的絕對URL,我問我如何獲得絕對URL後再顯示它。我使用jsoup庫作爲網頁抓取工具。顯示從jsoup網站抓取的圖像
我用下面的代碼來獲得下面的圖片中顯示所需的輸出(使用合適的進口):
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);
對於調用setRequestProperty,到位MyAppName中使用任何字符串,它只是一個值在HTTP請求中的User-Agent屬性,您的應用做出
非常感謝,非常感謝! –
你有沒有想過,也許使用Web瀏覽R' – phatfingers