`

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 

分享到:
评论

相关推荐

    [AB PLC例程源码][MMS_044666]Translation N-A.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    kolesar_3cd_01_0716.pdf

    kolesar_3cd_01_0716

    latchman_01_0108.pdf

    latchman_01_0108

    matlab程序代码项目案例:matlab程序代码项目案例MPC在美国高速公路场景中移动的车辆上的实现.zip

    matlab程序代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    pimpinella_3cd_01_0716.pdf

    pimpinella_3cd_01_0716

    petrilla_01_0308.pdf

    petrilla_01_0308

    [AB PLC例程源码][MMS_041452]Speed Controls in Plastic Extrusion.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    强化学习驱动下DeepSeek技术创新及其对AI发展的影响

    内容概要:本文档由张卓老师讲解,重点探讨DeepSeek的技术革新及强化学习对未来AI发展的重要性。文章回顾了AI的历史与发展阶段,详细解析Transformer架构在AI上半场所起到的作用,深入介绍了MoE混合专家以及MLA低秩注意机制等技术特点如何帮助DeepSeek在AI中场建立优势,并探讨了当前强化学习的挑战和边界。文档不仅提及AlphaGo和小游戏等成功案例来说明强化学习的强大力量,还提出了关于未来人工通用智能(AGI)的展望,特别是如何利用强化学习提升现有LLMs的能力和性能。 适用人群:本资料适宜对深度学习感兴趣的研究人员、开发者以及想要深入了解人工智能最新进展的专业人士。 使用场景及目标:通过了解最新的AI技术和前沿概念,在实际工作中能够运用更先进的工具和技术解决问题。同时为那些寻求职业转型或者学术深造的人提供了宝贵的参考。 其他说明:文中提到了许多具体的例子和技术细节,如DeepSeek的技术特色、RL的理论背景等等,有助于加深读者对于现代AI系统的理解和认识。

    有师傅小程序开源版v2.4.14+前端.zip

    有师傅小程序开源版v2.4.14 新增报价短信奉告 优化部分细节

    [AB PLC例程源码][MMS_047333]Motor Sequence Starter with timers to start.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    商城二级三级分销系统(小程序+后台含源码).zip

    商城二级三级分销系统(小程序+后台含源码).zip

    li_3ck_01b_0918.pdf

    li_3ck_01b_0918

    nicholl_3cd_01_0516.pdf

    nicholl_3cd_01_0516

    1995-2022年 网络媒体关注度、报刊媒体关注度与媒体监督相关数据.zip

    媒体关注度是一个衡量公众对某个事件、话题或个体关注程度的重要指标。它主要反映了新闻媒体、社交媒体、博客等对于某一事件、话题或个体的报道和讨论程度。 媒体监督的J-F系数(Janis-Fadner系数)是一种用于测量媒体关注度的指标,特别是用于评估媒体对企业、事件或话题的监督力度。J-F系数基于媒体报道的正面和负面内容来计算,从而为公众、研究者或企业提供一个量化工具,以了解媒体对其关注的方向和强度。 本数据含原始数据、参考文献、代码do文件、最终结果。参考文献中JF系数计算公式。 指标 代码、年份、标题出现该公司的新闻总数、内容出现该公司的新闻总数、正面新闻数全部、中性新闻数全部、负面新闻数全部、正面新闻数原创、中性新闻数原创、负面新闻数原创,媒体监督JF系数。

    [AB PLC例程源码][MMS_040315]Double INC and Double DEC of INT datatype.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    [AB PLC例程源码][MMS_047773]Convert Feet to Millimeters.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    [AB PLC例程源码][MMS_042349]How to read-write data to-from a PLC using OPC in Visual Basic 6.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    matlab程序代码项目案例:matlab程序代码项目案例论文代码 多篇RMPC 鲁棒模型预测控制Paper-code-implementation.zip

    matlab程序代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    lusted_3cd_02_0716.pdf

    lusted_3cd_02_0716

    pepeljugoski_01_0107.pdf

    pepeljugoski_01_0107

Global site tag (gtag.js) - Google Analytics