无法显示localhost中的图像

时间:2011-05-13 09:05:43

标签: java image swing applet

我不明白为什么在我的webAPP中当我尝试通过这样做来加载一个Icon例如:

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

rtfIcon.getImageLoadStatus()返回一个ERRORED值。

但在我的网络浏览器中,它正确显示http://localhost:8080/app/images/rtf.gif。 我不懂。我用apache tomcat。是否有任何选项配置或简单的编码错误?

提前感谢。

1 个答案:

答案 0 :(得分:3)

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

假设String指的是 File改为使用..

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

..或考虑到BalusC的建议..

  

“只要Web服务器在物理上不同的机器上运行(在生产中),applet将无法工作。而是使用getCodeBase()获取到提供applet的主机的URL,然后进一步构建该“。

ImageIcon rtfIcon = new ImageIcon(
    new URL(getCodeBase(), "/app/images/rtf.gif"));
相关问题