0

如果我执行以下 Groovy 代码

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

我得到一个例外:

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

但是如果我在浏览器中访问 URL,图像就会显示出来。是因为 HTTP 请求被阻止是因为它(从标头)看起来不像来自浏览器吗?

4

2 回答 2

2

用这个:

 Image image = Toolkit.getDefaultToolkit().createImage(url);
于 2012-07-31T14:39:41.267 回答
0

使用下面的代码作为参考。做类似的事情。

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


                    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);
                    }
                    fout.close();
                    bins.close();

                    //System.out.println("File " +lastWord +" downloaded successfully ...\n\n ");   
                    LOG.info("File " +lastWord +" downloaded successfully");
于 2012-07-31T14:39:58.727 回答