简体   繁体   中英

Display image from webpage in java

So I want to make some kind of slide show that shows the picture from a pixiv page. The basic idea is that I give the program the link to the page that I want to show ( https://www.pixiv.net/en/artworks/85339340 ) and then I want my program to only show the image I first tried doing it like this:

 public TestURLImage() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            try {
                String path = "https://i.pximg.net/img-master/img/2020/10/31/00/13/28/85339340_p0_master1200.jpg";
                System.out.println("Get Image from " + path);
                URL url = new URL(path);
                BufferedImage image = ImageIO.read(url);
                System.out.println("Load image into frame...");
                JLabel label = new JLabel(new ImageIcon(image));
                JFrame f = new JFrame();
              //  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.getContentPane().add(label);
                f.pack();
                f.setLocation(200, 200);
                f.setVisible(true);
            } catch (Exception exp) {
                exp.printStackTrace();
            }

        }
    });
}

but that gave me this error:

javax.imageio.IIOException: Can't get input stream from URL!
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1407)
    at TestURLImage$1.run(TestURLImage.java:27)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
 Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://i.pximg.net/img-master/img/2020/10/31/00/13/28/85339340_p0_master1200.jpg   
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1932)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1528)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
    at java.base/java.net.URL.openStream(URL.java:1167)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1405)
    ... 14 more

So like I said my idea is now to just get the entire page and just show the image, but I have no idea how to do that, could you please help with this.

If you read the exception message carefully, this part here:

 Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://i.pximg.net/img-master/img/2020/10/31/00/13/28/85339340_p0_master1200.jpg   
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1932)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1528)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
    at java.base/java.net.URL.openStream(URL.java:1167)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1405)
    ... 14 more

explains URL is returning 403 Forbidden which means you can't access image directly.

When you open a website, some cookies are received and stored by your browser and sent back to the server when getting images, etc. Searching for web scraping techniques may lead you to a solution, but that's all i can tell and i can't help you further because this way of accessing a website's resources is probably piracy and is illegal under certain circumstances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM