如果我执行以下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请求被阻止了,因为它看起来不像来自浏览器的(来自标题)?
答案 0 :(得分:2)
使用此:
Image image = Toolkit.getDefaultToolkit().createImage(url);
答案 1 :(得分: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");