Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

eclipse - Saving a PNG file on a server in a Java bean, using JSTL

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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=""/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...