highchart 导出中文问题

参考 :http://www.blogjava.net/xyzroundo/articles/186217.html

http://blog.csdn.net/zx404068203/article/details/6033040

https://github.com/highslide-software/highcharts.com/issues/1323

http://www.douban.com/note/242998670/

需要的包:highchart 导出中文问题_第1张图片

servlet :

/*
 * @(#)HighChartsServlet.java	V0.1 2013年10月16日
 *
 * Copyright 2013 Dreamity Software, Inc. All rights reserved.
 * This software is the confidential and proprietary information
 * of Dreamity Software, Inc. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Dreamity Software, Inc.
 */
package com.nari.slsd.hw.servlet;

/**
 * @Package:com.nari.slsd.hw.servlet
 * @Title:HighChartsServlet.java
 * @Description: TODO
 * @Author:terry
 * @Date:2013年10月16日下午4:32:15
 * @Version:V0.1
 */
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.fop.svg.PDFTranscoder;

/**
 * Servlet implementation class for the batik Transcoder
 */
public class HighChartsServlet extends HttpServlet {

    private static final long serialVersionUID = 3920224595120519682L;

    public HighChartsServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//注意编码  
        // 先试着通过request.getParameter来获取导出的参数
        // 此时请求必须是GET或者POST且Content-Type是application/x-www-form-urlencoded
        // 这里无需进行转码, 因为com.highcharts.export.filters.DefaultEncodingFilter已经做了处理
        // chrome中Highcharts POST提交的表单Content-Type会是multipart/form-data, 通过request.getParameter是无法获得表单的值(因此此时属于文件上传)
        // 这是正常的, 因为modules/exporting.js#exportChart()就是这么设置的enctype: 'multipart/form-data'
        // Adding enctype to the exporting post to allow it to work in HTTPS in Firefox. Issue #839.
        // https://github.com/highslide-software/highcharts.com/issues/839
        // 但是IE中Highcharts POST提交的表单Content-Type会变成application/x-www-form-urlencoded, 又只能通过request.getParameter获得表单的值
        // 如果使用ServletFileUpload.parseRequest会直接报错 the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
        // 结果这又是IE的bug, JavaScript动态创建的表单即使设置了enctype为'multipart/form-data'也无济于事, 直接写在HTML中的form元素设置enctype="multipart/form-data"又可以!
        // 必须设置encoding为multipart/form-data
        String type = request.getParameter("type");
        String svg = request.getParameter("svg");
        String filename = request.getParameter("filename");
        String width = request.getParameter("width");
        // chrome中Highcharts POST提交的表单Content-Type会是multipart/form-data, 通过request.getParameter是无法获得表单的值(因此此时属于文件上传)
        // 这是正常的, 因为modules/exporting.js#exportChart()就是这么设置的enctype: 'multipart/form-data'
        // 对IE采用request.getParameter来获取参数值, chrome采用commons-fileupload来获取表单值
        if(svg == null) {
            try {
                // 通过commons-fileupload来代替servlet 3.0中新的用于文件上传的API(getPart)
                DiskFileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                //判断提交表单的类型是否为multipart/form-data
                if(!upload.isMultipartContent(request)) {
                    return;
                }
                List items = upload.parseRequest(request);
                for(FileItem item : items) {
                    if(item.isFormField()) {
                        // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                        String fieldName = item.getFieldName();
                        // 经过fileupload处理过的request中的表单值需要转码, 否则中文会乱码, DefaultEncodingFilter对此无效
                        String fieldValue = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8");

                        if("svg".equals(fieldName)) {
                            svg = fieldValue;
                        } else if("filename".equals(fieldName)) {
                            filename = fieldValue;
                        } else if("width".equals(fieldName)) {
                            width = fieldValue;
                        } else if("type".equals(fieldName)) {
                            type = fieldValue;
                        }
                    }
                }
            } catch(FileUploadException e) {
            }
        }

        ServletOutputStream out = response.getOutputStream();
        if(null != type && null != svg) {
            // This line is necessary due to a bug in the highcharts SVG generator for IE  
            // I'm guessing it wont be needed later.  
            svg = svg.replaceAll(":rect", "rect");
            String ext = "";
            Transcoder t = null;

            if(type.equals("image/png")) {
                ext = "png";
                t = new PNGTranscoder();

            } else if(type.equals("image/jpeg")) {
                ext = "jpg";
                t = new JPEGTranscoder();

            } else if(type.equals("application/pdf")) {
                ext = "pdf";
                t = new PDFTranscoder();

            } else if(type.equals("image/svg+xml")) {
                ext = "svg";
            }

            response.addHeader("Content-Disposition", "attachment; filename=chart." + ext);
            response.addHeader("Content-Type", type);

            if(null != t) {
                TranscoderInput input = new TranscoderInput(new StringReader(svg));
                TranscoderOutput output = new TranscoderOutput(out);
                try {
                    t.transcode(input, output);
                } catch(TranscoderException e) {
                    out.print("Problem transcoding stream. See the web logs for more details.");
                    e.printStackTrace();
                }

            } else if(ext == "svg") {
//                out.print(svg);
                OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");  
                writer.append(svg);  
                writer.close(); 
            } else {
                out.print("Invalid type: " + type);
            }
        } else {
            response.addHeader("Content-Type", "text/html");
            out.println("Usage:\n\tParameter [svg]: The DOM Element to be converted.\n\tParameter [type]: The destination MIME type for the elment to be transcoded.");
        }
        out.flush();
        out.close();
    }

}


你可能感兴趣的:(java)