从URL获取图像(Java) [英] Getting Image from URL (Java)

查看:117
本文介绍了从URL获取图像(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读以下图片





但它显示IIOException。



这是代码:

  Image image = null; 
网址url =新网址(http://bks6.books.google.ca/books?id=5VTBuvfZDyoC&printsec=frontcover&img=1& zoom = 5& edge = curl& source = gbs_api) ;
image = ImageIO.read(url);
jXImageView1.setImage(image);


解决方案

您将获得 HTTP 400 (错误请求)错误,因为您的网址中有空格。如果您修复它(在 zoom 参数之前),您将收到 HTTP 400 错误(未授权)。
也许您需要一些HTTP标头来将您的下载标识为已识别的浏览器(使用User-Agent标头)或其他身份验证参数。



用户代理示例,然后使用 ImageIO.read(InputStream)

  URLConnection connection = url .openConnection(); 
connection.setRequestProperty(User-Agent,xxxxxx);

使用 xxxxxx

I am trying to read the following image

But it is showing IIOException.

Here is the code:

Image image = null;
URL url = new URL("http://bks6.books.google.ca/books?id=5VTBuvfZDyoC&printsec=frontcover&img=1& zoom=5&edge=curl&source=gbs_api");
image = ImageIO.read(url);
jXImageView1.setImage(image); 

解决方案

You are getting an HTTP 400 (Bad Request) error because there is a space in your URL. If you fix it (before the zoom parameter), you will get an HTTP 400 error (Unauthorized). Maybe you need some HTTP header to identify your download as a recognised browser (use the "User-Agent" header) or additional authentication parameter.

For the User-Agent example, then use the ImageIO.read(InputStream) using the connection inputstream:

URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "xxxxxx");

Use whatever needed for xxxxxx

这篇关于从URL获取图像(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆