无法显示从本地主机的图像 [英] Can't display images from localhost

查看:138
本文介绍了无法显示从本地主机的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在我的Web应用程序当我尝试做这行为例加载一个图标:

 的ImageIcon rtfIcon =新的ImageIcon(HTTP://本地主机:8080 /应用/图像/ rtf.gif);

在rtfIcon.getImageLoadStatus()返回一个差错值。

但在我的网页浏览器会显示正确的http://本地主机:8080 /应用/图像/ rtf.gif
我不明白这一点。我使用的Apache Tomcat。是否有任何选项配置做还是很简单的编码错误?

先谢谢了。


解决方案

 的ImageIcon rtfIcon =新的ImageIcon(HTTP://本地主机:8080 /应用/图像/ rtf.gif) ;

presumes的字符串 文件使用来代替。

 的ImageIcon rtfIcon =新的ImageIcon(
    新的URL(HTTP://本地主机:8080 /应用/图像/ rtf.gif));

..或者,考虑到从BalusC的意见。


  

applet将每当Web服务器在不同的物理机器上运行(生产)无法正常工作,而是使用得到codeBase的()来获取URL到该小程序从供应,然后再以此为基础的主机。


 的ImageIcon rtfIcon =新的ImageIcon(
    新的URL(获得codeBase的(),/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"));

这篇关于无法显示从本地主机的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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