无法显示来自本地主机的图像 [英] Can't display images from localhost
问题描述
我不明白为什么在我的 webAPP 中,当我尝试通过执行此操作加载图标时:
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");
rtfIcon.getImageLoadStatus() 返回错误值.
the rtfIcon.getImageLoadStatus() returns an ERRORED value .
但在我的网络浏览器中它正确显示 http://localhost:8080/app/images/rtf.gif .我不明白.我使用 apache tomcat .是否有任何选项配置要做或者是简单的编码错误?
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 ?
提前致谢.
推荐答案
ImageIcon rtfIcon = new ImageIcon("http://localhost:8080/app/images/rtf.gif");
假定 String
指的是 File
. 改为使用..
Presumes the String
refers to a File
. Use instead..
ImageIcon rtfIcon = new ImageIcon(
new URL("http://localhost:8080/app/images/rtf.gif"));
..或者,考虑到 BalusC 的建议..
..or, taking into account the advice from BalusC..
只要网络服务器在物理上不同的机器上运行(在生产中),小程序就会无法工作.而是使用 getCodeBase()
来获取到提供小程序的主机的 URL然后在此基础上进一步发展."
"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"));
这篇关于无法显示来自本地主机的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!