开发者

Can't display images from localhost

开发者 https://www.devze.com 2023-03-05 09:46 出处:网络
I dont understand why in my webAPP When I try to load an Icon by doing this for exemple : ImageIcon rtfIcon = new ImageIcon(\"http://localhost:8080/app/images/rtf.gif\");

I dont understand why in my webAPP When I try to load an Icon by doing this for exemple :

ImageIcon rtfIcon = new ImageIcon("http://localhost:8080/app/images/rtf.gif"); 

the rtfIcon.getImageLoadStatus() returns an ERRORED value .

But in my web browser it displays properly http://localhost:8080/app/images/rtf.gif . I dont get it . I use开发者_高级运维 apache tomcat . Is there any option configuration to do or it's simple coding mistake ?

thanks in advance .


ImageIcon rtfIcon = new ImageIcon("http://localhost:8080/app/images/rtf.gif"); 

Presumes the String refers to a File. Use instead..

ImageIcon rtfIcon = new ImageIcon(
    new URL("http://localhost:8080/app/images/rtf.gif"));

..or, taking into account the advice from BalusC..

"The applet will fail to work whenever the webserver runs at a physically different machine (in production). Rather use getCodeBase() to get the URL to the host where the applet is been served from and then build further on that."

ImageIcon rtfIcon = new ImageIcon(
    new URL(getCodeBase(), "/app/images/rtf.gif"));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号