开发者

Displaying Images in Tomcat Portlet

I currently have a portlet with a file explorer and a blank pane. When the user selects to open an image file, I would like to display the image in the pane.

However, the image exists in /home/myUser/images/ and the portlet exists in /home/server/tomcat/tomcat-6.0.18/webapps/mycompany. It is placed there by hotdelploying a portlet through Liferay. Basically, in the code i want to be able to geenerate some html to display that image. However, I know I cant just say

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

from within my portlet. So, I thought about copying it over into the tomcat-6.0.18/temp directory using the File.createTempFile method. I successfully copy the file there, and it is there. However, when I now say

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

I still cant display it! Note: the text above comes from:

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() + "'/>";

Please be detailed in how I can fix this problem!

Thanks!

EDIT: I have come across some stuff about an Image Servlet? Any ideas?


The path to the image must be web accessible. Using File#createTempFile will save the image into a non web accessible place like:

/var/cache/tomcat6/smiley.png

Try this instead:

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

Now save it to smileyPath. This will be write it to a path like /usr/share/tomcat6/webapps/<your-web-app>/smiley.png

Then in your HTML you can reference the image like:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜