2017-04-18 87 views
1

我正在Java中制作一个小型应用程序,需要我从网站上刮取图像并将其显示在GUI中。现在我不问如何获取图像的绝对URL,我问我如何获得绝对URL后再显示它。我使用jsoup库作为网页抓取工具。显示从jsoup网站抓取的图像

+0

你有没有想过,也许使用Web浏览R' – phatfingers

回答

0

我用下面的代码来获得下面的图片中显示所需的输出(使用合适的进口):

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属性,您的应用做出

Reference Image

+1

非常感谢,非常感谢! –