servletutilities.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 445 行 · 第 1/2 页

JAVA
445
字号
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. * * Project Info:  http://www.jfree.org/jfreechart/index.html * * This library is free software; you can redistribute it and/or modify it  * under the terms of the GNU Lesser General Public License as published by  * the Free Software Foundation; either version 2.1 of the License, or  * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public  * License for more details. * * You should have received a copy of the GNU Lesser General Public License  * along with this library; if not, write to the Free Software Foundation,  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc.  * in the United States and other countries.] * * --------------------- * ServletUtilities.java * --------------------- * (C) Copyright 2002-2005, by Richard Atkinson and Contributors. * * Original Author:  Richard Atkinson; * Contributor(s):   J?rgen Hoffman; *                   David Gilbert (for Object Refinery Limited); *                   Douglas Clayton; * * $Id: ServletUtilities.java,v 1.3 2005/03/28 19:53:47 mungady Exp $ * * Changes * ------- * 19-Aug-2002 : Version 1; * 20-Apr-2003 : Added additional sendTempFile method to allow MIME type  *               specification and modified original sendTempFile method to  *               automatically set MIME type for JPEG and PNG files * 23-Jun-2003 : Added additional sendTempFile method at the request of  *               J?rgen Hoffman; * 07-Jul-2003 : Added more header information to streamed images; * 19-Aug-2003 : Forced images to be stored in the temporary directory defined  *               by System property java.io.tmpdir, rather than default (RA); * 24-Mar-2004 : Added temp filename prefix attribute (DG); * 09-Mar-2005 : Added "one time" file option (DG); * */package org.jfree.chart.servlet;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 java.util.TimeZone;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-";        /** A prefix for "one time" charts. */    private static String tempOneTimeFilePrefix = "jfreechart-onetime-";        /**     * 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;    }        /**     * Returns the prefix for "one time" temporary file names generated by     * this class.     *      * @return The prefix.     */    public static String getTempOneTimeFilePrefix() {        return ServletUtilities.tempOneTimeFilePrefix;    }        /**     * Sets the prefix for the "one time" temporary file names generated by      * this class.     *      * @param prefix  the prefix (<code>null</code> not permitted).     */    public static void setTempOneTimeFilePrefix(String prefix) {        if (prefix == null) {            throw new IllegalArgumentException("Null 'prefix' argument.");           }        ServletUtilities.tempOneTimeFilePrefix = 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 (if <code>null</code>, the     *                 temporary file is marked as "one-time" and deleted by      *                 the {@link DisplayChart} servlet right after it is     *                 streamed to 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 {@link 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 (if <code>null</code>, the     *                 temporary file is marked as "one-time" and deleted by      *                 the {@link DisplayChart} servlet right after it is     *                 streamed to 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();        String prefix = ServletUtilities.tempFilePrefix;        if (session == null) {            prefix = ServletUtilities.tempOneTimeFilePrefix;        }        File tempFile = File.createTempFile(            prefix, ".png", new File(System.getProperty("java.io.tmpdir"))        );        ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);        if (session != null) {            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 (if <code>null</code>, the     *                 temporary file is marked as "one-time" and deleted by      *                 the {@link DisplayChart} servlet right after it is     *                 streamed to 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        );            }    /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?