`

jfreechart出现ChartDeleter没有序列化问题导致session信息丢失的解决方法

    博客分类:
  • java
阅读更多
jfreechart0.9版本的在生成图表后出现ChartDeleter没有序列化问题的解决方法,错误信息如下

view plaincopy to clipboardprint?
2009-09-01 16:53:52,066 [] ERROR encoder.EncryptCookieEncoderImpl - Failed to encode cookie state   
java.io.NotSerializableException: org.jfree.chart.servlet.ChartDeleter   
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)   
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)   
        at java.util.HashMap.writeObject(HashMap.java:1001)   
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)   
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)   
        at java.lang.reflect.Method.invoke(Method.java:597)   
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)   
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)   
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)   
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)   
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)  
2009-09-01 16:53:52,066 [] ERROR encoder.EncryptCookieEncoderImpl - Failed to encode cookie state
java.io.NotSerializableException: org.jfree.chart.servlet.ChartDeleter
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
        at java.util.HashMap.writeObject(HashMap.java:1001)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1461)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326) 

这样图片是能正常显示的,但是这样可能会导致session原有的信息丢失,是因为ChartDeleter是传入session时如果没有经过序列化处理,反序列化回来的时候会破坏session的其它信息。

解决方法如下:把jfreechart的下面两个类取出来,放到自己的工程里

view plaincopy to clipboardprint?
import java.io.BufferedInputStream;   
import java.io.BufferedOutputStream;   
import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.IOException;   
import java.text.SimpleDateFormat;   
import java.util.Date;   
import javax.servlet.http.HttpServletResponse;   
import javax.servlet.http.HttpSession;   
import org.jfree.chart.ChartRenderingInfo;   
import org.jfree.chart.ChartUtilities;   
import org.jfree.chart.JFreeChart;   
/**  
 * Utility class used for servlet related JFreeChart operations.  
 *  
 * @author Richard Atkinson  
 */  
public class ServletUtilities {   
    /** The filename prefix. */  
    private static String tempFilePrefix = "jfreechart-";   
       
    /**  
     * Returns the prefix for the temporary file names generated by this class.  
     *   
     * @return The prefix (never <code>null</code>).  
     */  
    public static String getTempFilePrefix() {   
        return ServletUtilities.tempFilePrefix;      
    }   
       
    /**  
     * Sets the prefix for the temporary file names generated by this class.  
     *   
     * @param prefix  the prefix (<code>null</code> not permitted).  
     */  
    public static void setTempFilePrefix(String prefix) {   
        if (prefix == null) {   
            throw new IllegalArgumentException("Null 'prefix' argument.");      
        }   
        ServletUtilities.tempFilePrefix = prefix;   
    }   
       
    /**  
     * Saves the chart as a PNG format file in the temporary directory.  
     *  
     * @param chart  the JFreeChart to be saved.  
     * @param width  the width of the chart.  
     * @param height  the height of the chart.  
     * @param session  the HttpSession of the client.  
     *  
     * @return the filename of the chart saved in the temporary directory.  
     *  
     * @throws IOException if there is a problem saving the file.  
     */  
    public static String saveChartAsPNG(JFreeChart chart, int width, int height,   
                                        HttpSession session) throws IOException {   
        return ServletUtilities.saveChartAsPNG(chart, width, height, null, session);   
           
    }   
    /**  
     * Saves the chart as a PNG format file in the temporary directory and  
     * populates the ChartRenderingInfo object which can be used to generate  
     * an HTML image map.  
     *  
     * @param chart  the chart to be saved (<code>null</code> not permitted).  
     * @param width  the width of the chart.  
     * @param height  the height of the chart.  
     * @param info  the ChartRenderingInfo object to be populated (<code>null</code> permitted).  
     * @param session  the HttpSession of the client.  
     *  
     * @return the filename of the chart saved in the temporary directory.  
     *  
     * @throws IOException if there is a problem saving the file.  
     */  
    public static String saveChartAsPNG(JFreeChart chart, int width, int height,   
                                        ChartRenderingInfo info, HttpSession session)   
            throws IOException {   
        if (chart == null) {   
            throw new IllegalArgumentException("Null 'chart' argument.");      
        }   
        ServletUtilities.createTempDir();   
        File tempFile = File.createTempFile(   
            ServletUtilities.tempFilePrefix, ".png",    
            new File(System.getProperty("java.io.tmpdir"))   
        );   
        ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);   
        ServletUtilities.registerChartForDeletion(tempFile, session);   
        return tempFile.getName();   
    }   
    /**  
     * Saves the chart as a JPEG format file in the temporary directory.  
     *  
     * @param chart  the JFreeChart to be saved.  
     * @param width  the width of the chart.  
     * @param height  the height of the chart.  
     * @param session  the HttpSession of the client.  
     *  
     * @return the filename of the chart saved in the temporary directory.  
     *  
     * @throws IOException if there is a problem saving the file.  
     */  
    public static String saveChartAsJPEG(JFreeChart chart, int width, int height,   
                                         HttpSession session) throws IOException {   
        return ServletUtilities.saveChartAsJPEG(chart, width, height, null, session);   
           
    }   
    /**  
     * Saves the chart as a JPEG format file in the temporary directory and  
     * populates the ChartRenderingInfo object which can be used to generate  
     * an HTML image map.  
     *  
     * @param chart  the chart to be saved (<code>null</code> not permitted).  
     * @param width  the width of the chart  
     * @param height  the height of the chart  
     * @param info  the ChartRenderingInfo object to be populated  
     * @param session  the HttpSession of the client  
     *  
     * @return the filename of the chart saved in the temporary directory  
     *  
     * @throws IOException if there is a problem saving the file.  
     */  
    public static String saveChartAsJPEG(JFreeChart chart, int width, int height,   
                                         ChartRenderingInfo info, HttpSession session)   
            throws IOException {   
        if (chart == null) {   
            throw new IllegalArgumentException("Null 'chart' argument.");      
        }   
           
        ServletUtilities.createTempDir();   
        File tempFile = File.createTempFile(   
            ServletUtilities.tempFilePrefix,    
            ".jpeg", new File(System.getProperty("java.io.tmpdir"))   
        );   
        ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);   
        ServletUtilities.registerChartForDeletion(tempFile, session);   
        return tempFile.getName();   
    }   
    /**  
     * Creates the temporary directory if it does not exist.  
     * Throws a RuntimeException if the temporary directory is null.  
     * Uses the system property java.io.tmpdir as the temporary directory.  
     * Sounds like a strange thing to do but my temporary directory was not created  
     * on my default Tomcat 4.0.3 installation.  Could save some questions on the  
     * forum if it is created when not present.  
     */  
    protected static void createTempDir() {   
        String tempDirName = System.getProperty("java.io.tmpdir");   
        if (tempDirName == null) {   
            throw new RuntimeException(   
                "Temporary directory system property (java.io.tmpdir) is null");   
        }   
        //  Create the temporary directory if it doesn't exist   
        File tempDir = new File(tempDirName);   
        if (!tempDir.exists()) {   
            tempDir.mkdirs();   
        }   
    }   
    /**  
     * Adds a ChartDeleter object to the session object with the name JFreeChart_Deleter  
     * if there is not already one bound to the session and adds the filename to the  
     * list of charts to be deleted.  
     *  
     * @param tempFile  the file to be deleted.  
     * @param session  the HTTP session of the client.  
     */  
    protected static void registerChartForDeletion(File tempFile, HttpSession session) {   
        //  Add chart to deletion list in session   
        if (session != null) {   
            ChartDeleter chartDeleter = (ChartDeleter) session.getAttribute("JFreeChart_Deleter");   
            if (chartDeleter == null) {   
                chartDeleter = new ChartDeleter();   
                session.setAttribute("JFreeChart_Deleter", chartDeleter);   
            }   
            chartDeleter.addChart(tempFile.getName());   
        }   
        else {   
            System.out.println("Session is null - chart will not be deleted");   
        }   
    }   
    /**  
     * Binary streams the specified file in the temporary directory to the  
     * HTTP response in 1KB chunks  
     * @param filename The name of the file in the temporary directory.  
     * @param response The HTTP response object.  
     * @throws IOException  if there is an I/O problem.  
     */  
    public static void sendTempFile(String filename, HttpServletResponse response)   
        throws IOException {   
        File file = new File(System.getProperty("java.io.tmpdir"), filename);   
        ServletUtilities.sendTempFile(file, response);   
    }   
    /**  
     * Binary streams the specified file to the HTTP response in 1KB chunks  
     *  
     * @param file The file to be streamed.  
     * @param response The HTTP response object.  
     *  
     * @throws IOException  if there is an I/O problem.  
     */  
    public static void sendTempFile(File file, HttpServletResponse response)   
            throws IOException {   
        String mimeType = null;   
        String filename = file.getName();   
        if (filename.length() > 5) {   
            if (filename.substring(filename.length() - 5, filename.length()).equals(".jpeg")) {   
                mimeType = "image/jpeg";   
            }    
            else if (filename.substring(filename.length() - 4, filename.length()).equals(".png")) {   
                mimeType = "image/png";   
            }   
        }   
        ServletUtilities.sendTempFile(file, response, mimeType);   
    }   
    /**  
     * Binary streams the specified file to the HTTP response in 1KB chunks  
     *  
     * @param file The file to be streamed.  
     * @param response The HTTP response object.  
     * @param mimeType The mime type of the file, null allowed.  
     *  
     * @throws IOException  if there is an I/O problem.  
     */  
    public static void sendTempFile(File file, HttpServletResponse response,   
                                    String mimeType) throws IOException {   
        if (file.exists()) {   
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));   
            //  Set HTTP headers   
            if (mimeType != null) {   
                response.setHeader("Content-Type", mimeType);   
            }   
            response.setHeader("Content-Length", String.valueOf(file.length()));   
            SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");   
            response.setHeader("Last-Modified", sdf.format(new Date(file.lastModified())));   
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());   
            byte[] input = new byte[1024];   
            boolean eof = false;   
            while (!eof) {   
                int length = bis.read(input);   
                if (length == -1) {   
                    eof = true;   
                }    
                else {   
                    bos.write(input, 0, length);   
                }   
            }   
            bos.flush();   
            bis.close();   
            bos.close();   
        }   
        else {   
            throw new FileNotFoundException(file.getAbsolutePath());   
        }   
        return;   
    }   
    /**  
     * Perform a search/replace operation on a String  
     * There are String methods to do this since (JDK 1.4)  
     *  
     * @param inputString  the String to have the search/replace operation.  
     * @param searchString  the search String.  
     * @param replaceString  the replace String.  
     *  
     * @return the String with the replacements made.  
     */  
    public static String searchReplace(String inputString,   
                                       String searchString,   
                                       String replaceString) {   
        int i = inputString.indexOf(searchString);   
        if (i == -1) {   
            return inputString;   
        }   
        String r = "";   
        r += inputString.substring(0, i) + replaceString;   
        if (i + searchString.length() < inputString.length()) {   
            r += searchReplace(inputString.substring(i + searchString.length()),   
                               searchString,   
                               replaceString);   
        }   
        return r;   
    }   
}  
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
/**
 * Utility class used for servlet related JFreeChart operations.
 *
 * @author Richard Atkinson
 */
public class ServletUtilities {
    /** The filename prefix. */
    private static String tempFilePrefix = "jfreechart-";
    
    /**
     * Returns the prefix for the temporary file names generated by this class.
     * 
     * @return The prefix (never <code>null</code>).
     */
    public static String getTempFilePrefix() {
        return ServletUtilities.tempFilePrefix;   
    }
    
    /**
     * Sets the prefix for the temporary file names generated by this class.
     * 
     * @param prefix  the prefix (<code>null</code> not permitted).
     */
    public static void setTempFilePrefix(String prefix) {
        if (prefix == null) {
            throw new IllegalArgumentException("Null 'prefix' argument.");   
        }
        ServletUtilities.tempFilePrefix = prefix;
    }
    
    /**
     * Saves the chart as a PNG format file in the temporary directory.
     *
     * @param chart  the JFreeChart to be saved.
     * @param width  the width of the chart.
     * @param height  the height of the chart.
     * @param session  the HttpSession of the client.
     *
     * @return the filename of the chart saved in the temporary directory.
     *
     * @throws IOException if there is a problem saving the file.
     */
    public static String saveChartAsPNG(JFreeChart chart, int width, int height,
                                        HttpSession session) throws IOException {
        return ServletUtilities.saveChartAsPNG(chart, width, height, null, session);
        
    }
    /**
     * Saves the chart as a PNG format file in the temporary directory and
     * populates the ChartRenderingInfo object which can be used to generate
     * an HTML image map.
     *
     * @param chart  the chart to be saved (<code>null</code> not permitted).
     * @param width  the width of the chart.
     * @param height  the height of the chart.
     * @param info  the ChartRenderingInfo object to be populated (<code>null</code> permitted).
     * @param session  the HttpSession of the client.
     *
     * @return the filename of the chart saved in the temporary directory.
     *
     * @throws IOException if there is a problem saving the file.
     */
    public static String saveChartAsPNG(JFreeChart chart, int width, int height,
                                        ChartRenderingInfo info, HttpSession session)
            throws IOException {
        if (chart == null) {
            throw new IllegalArgumentException("Null 'chart' argument.");   
        }
        ServletUtilities.createTempDir();
        File tempFile = File.createTempFile(
            ServletUtilities.tempFilePrefix, ".png", 
            new File(System.getProperty("java.io.tmpdir"))
        );
        ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
        ServletUtilities.registerChartForDeletion(tempFile, session);
        return tempFile.getName();
    }
    /**
     * Saves the chart as a JPEG format file in the temporary directory.
     *
     * @param chart  the JFreeChart to be saved.
     * @param width  the width of the chart.
     * @param height  the height of the chart.
     * @param session  the HttpSession of the client.
     *
     * @return the filename of the chart saved in the temporary directory.
     *
     * @throws IOException if there is a problem saving the file.
     */
    public static String saveChartAsJPEG(JFreeChart chart, int width, int height,
                                         HttpSession session) throws IOException {
        return ServletUtilities.saveChartAsJPEG(chart, width, height, null, session);
        
    }
    /**
     * Saves the chart as a JPEG format file in the temporary directory and
     * populates the ChartRenderingInfo object which can be used to generate
     * an HTML image map.
     *
     * @param chart  the chart to be saved (<code>null</code> not permitted).
     * @param width  the width of the chart
     * @param height  the height of the chart
     * @param info  the ChartRenderingInfo object to be populated
     * @param session  the HttpSession of the client
     *
     * @return the filename of the chart saved in the temporary directory
     *
     * @throws IOException if there is a problem saving the file.
     */
    public static String saveChartAsJPEG(JFreeChart chart, int width, int height,
                                         ChartRenderingInfo info, HttpSession session)
            throws IOException {
        if (chart == null) {
            throw new IllegalArgumentException("Null 'chart' argument.");   
        }
        
        ServletUtilities.createTempDir();
        File tempFile = File.createTempFile(
            ServletUtilities.tempFilePrefix, 
            ".jpeg", new File(System.getProperty("java.io.tmpdir"))
        );
        ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
        ServletUtilities.registerChartForDeletion(tempFile, session);
        return tempFile.getName();
    }
    /**
     * Creates the temporary directory if it does not exist.
     * Throws a RuntimeException if the temporary directory is null.
     * Uses the system property java.io.tmpdir as the temporary directory.
     * Sounds like a strange thing to do but my temporary directory was not created
     * on my default Tomcat 4.0.3 installation.  Could save some questions on the
     * forum if it is created when not present.
     */
    protected static void createTempDir() {
        String tempDirName = System.getProperty("java.io.tmpdir");
        if (tempDirName == null) {
            throw new RuntimeException(
                "Temporary directory system property (java.io.tmpdir) is null");
        }
        //  Create the temporary directory if it doesn't exist
        File tempDir = new File(tempDirName);
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }
    }
    /**
     * Adds a ChartDeleter object to the session object with the name JFreeChart_Deleter
     * if there is not already one bound to the session and adds the filename to the
     * list of charts to be deleted.
     *
     * @param tempFile  the file to be deleted.
     * @param session  the HTTP session of the client.
     */
    protected static void registerChartForDeletion(File tempFile, HttpSession session) {
        //  Add chart to deletion list in session
        if (session != null) {
            ChartDeleter chartDeleter = (ChartDeleter) session.getAttribute("JFreeChart_Deleter");
            if (chartDeleter == null) {
                chartDeleter = new ChartDeleter();
                session.setAttribute("JFreeChart_Deleter", chartDeleter);
            }
            chartDeleter.addChart(tempFile.getName());
        }
        else {
            System.out.println("Session is null - chart will not be deleted");
        }
    }
    /**
     * Binary streams the specified file in the temporary directory to the
     * HTTP response in 1KB chunks
     * @param filename The name of the file in the temporary directory.
     * @param response The HTTP response object.
     * @throws IOException  if there is an I/O problem.
     */
    public static void sendTempFile(String filename, HttpServletResponse response)
        throws IOException {
        File file = new File(System.getProperty("java.io.tmpdir"), filename);
        ServletUtilities.sendTempFile(file, response);
    }
    /**
     * Binary streams the specified file to the HTTP response in 1KB chunks
     *
     * @param file The file to be streamed.
     * @param response The HTTP response object.
     *
     * @throws IOException  if there is an I/O problem.
     */
    public static void sendTempFile(File file, HttpServletResponse response)
            throws IOException {
        String mimeType = null;
        String filename = file.getName();
        if (filename.length() > 5) {
            if (filename.substring(filename.length() - 5, filename.length()).equals(".jpeg")) {
                mimeType = "image/jpeg";
            } 
            else if (filename.substring(filename.length() - 4, filename.length()).equals(".png")) {
                mimeType = "image/png";
            }
        }
        ServletUtilities.sendTempFile(file, response, mimeType);
    }
    /**
     * Binary streams the specified file to the HTTP response in 1KB chunks
     *
     * @param file The file to be streamed.
     * @param response The HTTP response object.
     * @param mimeType The mime type of the file, null allowed.
     *
     * @throws IOException  if there is an I/O problem.
     */
    public static void sendTempFile(File file, HttpServletResponse response,
                                    String mimeType) throws IOException {
        if (file.exists()) {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            //  Set HTTP headers
            if (mimeType != null) {
                response.setHeader("Content-Type", mimeType);
            }
            response.setHeader("Content-Length", String.valueOf(file.length()));
            SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
            response.setHeader("Last-Modified", sdf.format(new Date(file.lastModified())));
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            byte[] input = new byte[1024];
            boolean eof = false;
            while (!eof) {
                int length = bis.read(input);
                if (length == -1) {
                    eof = true;
                } 
                else {
                    bos.write(input, 0, length);
                }
            }
            bos.flush();
            bis.close();
            bos.close();
        }
        else {
            throw new FileNotFoundException(file.getAbsolutePath());
        }
        return;
    }
    /**
     * Perform a search/replace operation on a String
     * There are String methods to do this since (JDK 1.4)
     *
     * @param inputString  the String to have the search/replace operation.
     * @param searchString  the search String.
     * @param replaceString  the replace String.
     *
     * @return the String with the replacements made.
     */
    public static String searchReplace(String inputString,
                                       String searchString,
                                       String replaceString) {
        int i = inputString.indexOf(searchString);
        if (i == -1) {
            return inputString;
        }
        String r = "";
        r += inputString.substring(0, i) + replaceString;
        if (i + searchString.length() < inputString.length()) {
            r += searchReplace(inputString.substring(i + searchString.length()),
                               searchString,
                               replaceString);
        }
        return r;
    }
}
 和

view plaincopy to clipboardprint?
import java.io.File;   
import java.util.Iterator;   
import java.util.List;   
import javax.servlet.http.HttpSessionBindingEvent;   
import javax.servlet.http.HttpSessionBindingListener;   
/**  
 * Used for deleting charts from the temporary directory when the users session  
 * expires.  
 *  
 * @author Richard Atkinson  
 */  
public class ChartDeleter implements HttpSessionBindingListener{   
    /** The chart names. */  
    private List chartNames = new java.util.ArrayList();   
    /**  
     * Blank constructor.  
     */  
    public ChartDeleter() {   
        super();   
    }   
    /**  
     * Add a chart to be deleted when the session expires  
     *  
     * @param filename  the name of the chart in the temporary directory to be deleted.  
     */  
    public void addChart(String filename) {   
        this.chartNames.add(filename);   
    }   
    /**  
     * Checks to see if a chart is in the list of charts to be deleted  
     *  
     * @param filename  the name of the chart in the temporary directory.  
     *  
     * @return a boolean value indicating whether the chart is present in the list.  
     */  
    public boolean isChartAvailable(String filename) {   
        return (this.chartNames.contains(filename));   
    }   
    /**  
     * Binding this object to the session has no additional effects.  
     *  
     * @param event  the session bind event.  
     */  
    public void valueBound(HttpSessionBindingEvent event) {   
        return;   
    }   
    /**  
     * When this object is unbound from the session (including upon session  
     * expiry) the files that have been added to the ArrayList are iterated  
     * and deleted.  
     *  
     * @param event  the session unbind event.  
     */  
    public void valueUnbound(HttpSessionBindingEvent event) {   
        Iterator iter = this.chartNames.listIterator();   
        while (iter.hasNext()) {   
            String filename = (String) iter.next();   
            File file = new File(System.getProperty("java.io.tmpdir"), filename);   
            if (file.exists()) {   
                file.delete();   
            }   
        }   
        return;   
    }   
}  
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/**
 * Used for deleting charts from the temporary directory when the users session
 * expires.
 *
 * @author Richard Atkinson
 */
public class ChartDeleter implements HttpSessionBindingListener{
    /** The chart names. */
    private List chartNames = new java.util.ArrayList();
    /**
     * Blank constructor.
     */
    public ChartDeleter() {
        super();
    }
    /**
     * Add a chart to be deleted when the session expires
     *
     * @param filename  the name of the chart in the temporary directory to be deleted.
     */
    public void addChart(String filename) {
        this.chartNames.add(filename);
    }
    /**
     * Checks to see if a chart is in the list of charts to be deleted
     *
     * @param filename  the name of the chart in the temporary directory.
     *
     * @return a boolean value indicating whether the chart is present in the list.
     */
    public boolean isChartAvailable(String filename) {
        return (this.chartNames.contains(filename));
    }
    /**
     * Binding this object to the session has no additional effects.
     *
     * @param event  the session bind event.
     */
    public void valueBound(HttpSessionBindingEvent event) {
        return;
    }
    /**
     * When this object is unbound from the session (including upon session
     * expiry) the files that have been added to the ArrayList are iterated
     * and deleted.
     *
     * @param event  the session unbind event.
     */
    public void valueUnbound(HttpSessionBindingEvent event) {
        Iterator iter = this.chartNames.listIterator();
        while (iter.hasNext()) {
            String filename = (String) iter.next();
            File file = new File(System.getProperty("java.io.tmpdir"), filename);
            if (file.exists()) {
                file.delete();
            }
        }
        return;
    }
}
 

然后修改ChartDeleter类,使其实现java.io.Serializable接口就ok了,其它东西不需要改动。

view plaincopy to clipboardprint?
public class ChartDeleter implements HttpSessionBindingListener, java.io.Serializable 

分享到:
评论

相关推荐

    Linux下Jfreechart会出现方框框

    如果问题仍然存在,可能需要检查JFreeChart的版本,因为它可能与特定的字体兼容性问题有关,或者升级到最新版本可能解决问题。 总之,当在Linux下使用JFreeChart遇到“方框框”问题时,通常是因为字体不兼容或缺失...

    JFreeChart乱码解决方法

    JFreeChart乱码解决方法 - 企业应用 - Java

    JFreeChart异步加载和解决缓存问题

    在处理大量数据或复杂的图表时,JFreeChart的性能可能会成为瓶颈,特别是在UI更新时,可能会导致用户界面阻塞,影响用户体验。因此,异步加载和缓存策略是优化JFreeChart性能的关键。 **异步加载** 异步加载是解决...

    jfreechart在LINUX下乱码问题的解决

    标签中的“源码”可能暗示了解决问题需要查看JFreeChart的源代码,理解其内部如何处理字符编码。“工具”可能指的是利用一些辅助工具,如IDE的调试功能、字符集转换工具等,来定位和解决问题。 至于压缩包中的...

    解决jfreechart在linux下无法显示中文

    通过以上方法,一般能够解决JFreeChart在Linux下无法显示中文的问题。对于给定的文件“simsun.ttc”,这是一个包含多种简体中文字体的集合文件,可以在Java应用中加载它,以确保JFreeChart在运行时能正确显示中文。 ...

    JFreeChart详细中文教程|案例|中文API|乱码解决方法|等集合

    绝对物超所值! JFreeChart-1.0.13原文件含jar包。 1、JFreeChart生成图片路径教程 2、JFreeChart使用教程(含下载安装,超详细...5、JFreeChart乱码解决方法 等等 包括csdn上得优秀案例 ------20+M的数据不要别后悔哦

    JFreechart 在linux下不显示及中文乱码问题

    标题“JFreechart在Linux下不显示及中文乱码问题”指出的是在使用JFreechart库创建图表时,在Linux操作系统环境下遇到的两个主要问题:图表无法正常显示以及中文字符出现乱码。JFreechart是一个Java库,用于生成高...

    Jfreechart

    通过以上步骤和注意事项,可以有效地解决 JFreeChart 在创建图表时出现的乱码问题。正确设置字体不仅有助于避免乱码,还能提升图表的可读性和美观度。在实际开发中,还应结合具体需求进行调试和优化,以实现最佳的...

    jfreechart中文乱码问题解决方案

    下面将详细阐述如何解决JFreeChart中文乱码问题,并提供不同图表类型的具体配置示例。 1. **柱状图(CategoryPlot)**:在柱状图中,我们需要设置`CategoryPlot`的`domainAxis`和`rangeAxis`的字体,以及图例的字体。...

    JFreeChart 详细中文教程 案例 中文API 乱码解决方法

    JFreeChart-1.0.13原文件含jar包。 1、JFreeChart生成图片路径教程 2、JFreeChart使用教程(含下载安装,超详细) 3、JFreeChart使用详细教程 ...5、JFreeChart乱码解决方法 等等 包括许多优秀案例

    jfreechart demo

    在学习过程中,博主的博客记录也是一个很好的资源,它可能包含了使用 JFreeChart 的心得、技巧以及遇到问题的解决方案。通过阅读这些记录,你可以进一步加深对 JFreeChart 的理解和应用。 总之,JFreeChart 是 Java...

    JFreeChart

    解决JFreeChart中的乱码问题通常需要以下几个步骤: 1. **设置编码**:确保你的源代码文件保存为正确的字符集,例如UTF-8。 2. **环境变量**:检查JVM启动参数,确保有正确的字符集设置,如`-Dfile.encoding=UTF-8...

    JFreechart存储为PDF不能显示中文文字的解决办法

    《JFreechart生成PDF中文显示问题的解决方案》 在使用JFreechart库生成PDF文档时,经常遇到一个棘手的问题,即中文文字无法正常显示。这主要是由于JFreechart默认的字体映射策略不支持中文字符集导致的。本文将详细...

    基于JFreeChart的股票交易K线图停牌日期缺口优化完整解决方案

    总的来说,这个解决方案利用JFreeChart库解决了股票K线图在遇到停牌日期时出现的缺口问题,提升了图表的可读性和分析效率。通过对CSV数据的处理、插值或标记方法的应用,以及JFreeChart的强大功能,我们可以得到连续...

    Jfreechart 中文乱码解决方法

    Jfreechart 中文乱码的解决方法,网上共享的资源

    jfreechart

    标题 "jfreechart" 指的是 JFreeChart,这是一个流行的开源 Java 图形库,用于创建高质量的图表。它在各种应用中广泛使用,包括报表、仪表盘、科学出版物等,提供了丰富的图表类型,如饼图、柱状图、线图、散点图和...

    解决jfreechart中文乱码方案整理.doc

    ### 解决JFreeChart中文乱码方案整理 #### 背景介绍 在使用JFreeChart绘制图表时,中文字符的显示常常会出现乱码问题。这不仅影响图表的美观度,也降低了数据可视化的效果。因此,针对JFreeChart中的中文乱码问题,...

    jfreechart图形化报表

    5. **社区支持**:JFreeChart 有一个活跃的开发者社区,不断更新和维护,遇到问题时可以寻求社区的帮助和解决方案。 **二、Jfreechart-1.0.13 版本介绍** 1. **版本更新**:jfreechart-1.0.13 是 JFreeChart 的一...

    zysong.ttf jfreechart 解决中文乱码 问题

    在IT行业中,尤其是在Java编程领域,使用图表库如JFreeChart进行数据可视化是常见的需求。然而,当在Linux环境中处理包含中文字符的数据时,可能会遇到中文乱码的问题。本文将详细探讨这个问题及其解决方案,主要...

    jfreechart-1.5.2.jar,jfreechart|jfreechart

    jfreechart-1.5.2.jar,jfreechart|jfreechart

Global site tag (gtag.js) - Google Analytics