【问题标题】:Displaying Images in Tomcat Portlet在 Tomcat Portlet 中显示图像
【发布时间】:2010-07-07 20:46:54
【问题描述】:

我目前有一个带有文件浏览器和空白窗格的 portlet。当用户选择打开图像文件时,我想在窗格中显示图像。

但是,图像存在于 /home/myUser/images/ 中,而 portlet 存在于 /home/server/tomcat/tomcat-6.0.18/webapps/mycompany 中。它通过 Liferay 热部署一个 portlet 放置在那里。基本上,在代码中,我希望能够生成一些 html 来显示该图像。但是,我知道我不能只说

<img src='/home/myUser/images/test.jpg'/> 

来自我的 portlet。因此,我考虑使用 File.createTempFile 方法将其复制到 tomcat-6.0.18/temp 目录中。我成功地将文件复制到那里,它就在那里。但是,当我现在说

<img src='/home/server/tomcat/tomcat-6.0.18/temp/test.jpg'/> 

还是不能显示!注:以上文字来自:

File tempImage = File.createTempFile("","");
FileReader in = new FileReader(myImageFile);
FileWriter out = new FileWriter(tempImage);
int c;

while ((c = in.read()) != -1)
    out.write(c);

in.close();
out.close();
String myHtmlString = "<img src='" + tempImage.getAbsolutePath() + "'/>";

请详细说明如何解决此问题!

谢谢!

编辑:我遇到了一些关于 Image Servlet 的东西?有什么想法吗?

【问题讨论】:

    标签: tomcat6


    【解决方案1】:

    图片的路径必须是可通过网络访问的。使用File#createTempFile 会将图像保存到无法通过网络访问的位置,例如:

    /var/cache/tomcat6/smiley.png

    试试这个:

    String smiley = "smiley.png";
    String smileyPath = getServletContext().getRealPath( smiley )
    

    现在将其保存到smileyPath。这会将其写入/usr/share/tomcat6/webapps/&lt;your-web-app&gt;/smiley.png

    之类的路径

    然后在您的 HTML 中,您可以像这样引用图像:

    String html = "<img src='" + smiley + "'/>"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2012-09-06
      相关资源
      最近更新 更多