leyendo imagen desde URL

If I execute the following Groovy code

URL url = new URL('http://glowstick.blisstunes.com/wp-content/plugins/rss-poster/cache/e1ebf_josh-wink.jpg')

Obtengo una excepción:

javax.imageio.IIOException: Can't get input stream from URL!
    at javax.imageio.ImageIO.read(ImageIO.java:1369)

But if I visit the URL in a browser the image displays. Is it because the HTTP request is being blocked because it doesn't look (from the headers) like it's coming from a browser?

preguntado el 31 de julio de 12 a las 15:07

That code works for me in the Groovy Console... :-/ -

@tim_yates it works for me in the Groovy Console too. It also works when I run the app in the dev env. But it doesn't work in the Grails console of the app in production. I suspect dragons are to blame. Curiously, the Toolkit suggestion below does work. -

@Don is the app server behind some sort of proxy? This seems to suggest a different workaround if you want to stick with ImageIO -

@tim_yates agreed, I don't know who he thinks he's impressing with those dreadlocks and wraparound shades. What he needs is a short back and sides and a pair of NHS specs. Kids today, pffff. -

2 Respuestas

Utilizar esta:

 Image image = Toolkit.getDefaultToolkit().createImage(url);

Respondido 31 Jul 12, 15:07

That seems to work, but unfortunately the code requires a RenderedImage en lugar de un Image. I've checked the implementation-type of this image y no es un RenderedImage. - Donal

Puede convertirlo en un BufferedImage! - elias

Use the below code as reference. Do similar thing .

                    URL urlTemp ;
                    urlTemp = new URL( ContentUrl);
                    HttpURLConnection ycGetContent = null;
                    ycGetContent = (HttpURLConnection) urlTemp.openConnection();
                    ycGetContent.setRequestProperty("Cookie", cooStr);

                    BufferedInputStream bins =
                            new BufferedInputStream(ycGetContent.getInputStream());

                    FileOutputStream fout =
                            new FileOutputStream(lastWord);
                    int m = 0;

                    byte[] bytesIn = new byte[1024];

                    while ((m = bins.read(bytesIn)) != -1) {
                        fout.write(bytesIn, 0, m);

                    //System.out.println("File " +lastWord +" downloaded successfully ...\n\n ");   
                    LOG.info("File " +lastWord +" downloaded successfully");

Respondido 31 Jul 12, 15:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.