使用JSTL在Java bean的服务器上保存PNG文件 [英] Saving a PNG file on a server in a Java bean, using JSTL
问题描述
ImageIO.write(myBufferedImage,png,新文件(C:/testChart.png));
我也可以将图像保存到我的tomcat目录,并在我在本地主机上运行网站。但是,一旦这个代码在不是我的本地主机的服务器上,保存到tomcat目录将不起作用。我如何将这个文件直接写入服务器...到我的Eclipse项目的WebContent文件夹(其中的java bean在src文件夹中)?感谢您提前获得任何帮助。
我建议您使用 ServletUtilities 类。它保存在java tempdir中,并在会话无效时进行清理。 :)然后显示文件的另一个提示是使用DisplayChart servlet来获取图像。
这在web.xml中
< servlet>
< servlet-name> DisplayChart< / servlet-name>
< servlet-class> org.jfree.chart.servlet.DisplayChart< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name> DisplayChart< / servlet-name>
< url-pattern> / servlet / DisplayChart< / url-pattern>
< / servlet-mapping>
然后,您如何使用jstl显示图像:
< img src =< c:url value ='/ servlet / DisplayChart?'>< c:param name ='filename'value ='$ {yourFileNameHERE}'/>< / c:url> ALT = />
I am writing an update page in JSTL where the user inputs some numbers. I then call a java bean - passing the numbers as parameters - which, using JFreeChart, creates a PNG image. All of this works fine when I save the file directly on my hard drive using
ImageIO.write(myBufferedImage, "png", new File("C:/testChart.png"));
I can also save the image to my tomcat directory and view it when I am running the website on my localhost. However, once this code is up on a server that is not my local host, saving to the tomcat directory won't work. How do I write this file directly to the server..say to the WebContent folder of my Eclipse project (where the java bean is in the src folder)? Thanks in advance for any help.
I would suggest that you use the ServletUtilities class. It saves in the java tempdir AND cleans up when the session is invalidated. :) Another hint for then displaying the file is to use the DisplayChart servlet to get your images. This goes in web.xml
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
This is then how you display the image using jstl:
<img src="<c:url value='/servlet/DisplayChart?'><c:param name='filename' value='${yourFileNameHERE}' /></c:url>" alt=""/>
这篇关于使用JSTL在Java bean的服务器上保存PNG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!