简体   繁体   English

如何在 JAVA FX 中从 Web 检索图像

[英]How to retrieve Image from Web in JAVA FX

This purpose of this code is to retrieve an Image from the web and then put in to a Image view in Java FX.此代码的目的是从 Web 检索图像,然后将其放入 Java FX 中的图像视图。 I am getting a mismatch error of Unresolved compilation problem: Type mismatch: cannot convert from Buffered Image to Image.我收到未解决的编译问题的不匹配错误:类型不匹配:无法从缓冲图像转换为图像。 How can I retrieve the Image on from the web then display it on to my Application?如何从网络上检索图像然后将其显示到我的应用程序上? What is this error mean for my Image, would this be a formatting issue?这个错误对我的图像意味着什么,这会是格式问题吗?

My Code我的代码

  URL url2 = new URL("http://mars.jpl.nasa.gov/msl-raw-images/proj/msl/redops/ods/surface/sol/01004/opgs/edr/fcam/FLB_486615455EDR_F0481570FHAZ00323M_.JPG");
    
    Image image = ImageIO.read(url2);
    
    imageV.setImage(image);

Don't try to convert the image using swing and imageio.不要尝试使用 swing 和 imageio 转换图像。 Also don't create a URL object.也不要创建 URL 对象。 None of that is necessary.这些都不是必需的。

Just load the image directly from the url string using the image constructor .只需使用图像构造函数直接从 url 字符串加载图像。

As noted in comments, you need to directly access the image on the server, not via a redirected location:如评论中所述,您需要直接访问服务器上的图像,而不是通过重定向位置:

if I change the protocol from http to https then it works.如果我将协议从 http 更改为 https,那么它就可以工作。 My guess is the website automatically upgrades to HTTPS, which I think results in a redirect, and I would not be surprised if JavaFX's image-loading implementation doesn't handle that.我的猜测是网站会自动升级到 HTTPS,我认为这会导致重定向,如果 JavaFX 的图像加载实现不能处理这个问题,我不会感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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