⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servletutilities.java

📁 提供JFreechart图表功能, 提供JFreechart图表功能,提供JFreechart图表功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2007, 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------------
 * ServletUtilities.java
 * ---------------------
 * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
 *
 * Original Author:  Richard Atkinson;
 * Contributor(s):   J?rgen Hoffman;
 *                   David Gilbert (for Object Refinery Limited);
 *                   Douglas Clayton;
 *
 * 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);
 * ------------- JFREECHART 1.0.x RELEASED ------------------------------------
 * 10-Jan-2006 : Updated API docs and reformatted (DG);
 * 13-Sep-2006 : Format date in response header in English, not locale default
 *               (see bug 1557141) (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.Locale;
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.
 */
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.
     * <p>
     * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
     * it is a "lossy" format that introduces visible distortions in the
     * resulting image - use PNG instead.  In addition, note that JPEG output
     * is supported by JFreeChart only for JRE 1.4.2 or later.
     * 
     * @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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -