论坛首页 Java企业应用论坛

HighCharts 导出图片的Action

浏览 5471 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-07-29  
最近刚用HighCharts来处理前端的图表,当我想将图片改出到本地的时候,遇到问题,在网上看了很多帖子,说的都是同样一个servlet导出的事。但我在使用中发现,如果我用IE就可以导出,用FF或者Chrome,则type和svg这二个参数死活没法传到servlet里,纠结的要命。不知道是不是和我的struts2设置编码为gb2312以及页面编码设置为gb2312有关,反正过程中,我尝试了编码转换,没成功。
后来,直接把servlet的内容写到Action里,应该就可以了吧。测试实现后,发现确实可行。实现代码贴出来,看有需要的,也可以看看。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;


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.fop.svg.PDFTranscoder;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

public class ImageExportAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{

    /**
     * 
     */
    private static final long serialVersionUID = 7547110498655678209L;
    
    private String svg;
    private String type;
    
    
    private String extName;
    
    @SuppressWarnings("unused")
    private HttpServletRequest request;
    @SuppressWarnings("unused")
    private HttpServletResponse response;
    
    
    private InputStream  inputStream;
    

    
    public String getContentDisposition() throws UnsupportedEncodingException {

        String s = new String("分析图".getBytes(), "ISO8859-1");

        return "attachment;filename=\"" + s + "."+getExtName()+"\"";
    }
    
    
    public String execute() throws Exception {

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        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";
        }

        setExtName(ext);

        if (null != t) {
            TranscoderInput input = new TranscoderInput(new StringReader(svg));
            TranscoderOutput output = new TranscoderOutput(out);
            try {
                t.transcode(input, output);
            } catch (TranscoderException e) {

                e.printStackTrace();
            }

        }
        
        out.close();
        byte[] data=out.toByteArray();

        inputStream= new ByteArrayInputStream(data); 
        
        return SUCCESS;

    }


    public String getSvg() {
        return svg;
    }


    public void setSvg(String svg) {
        this.svg = svg;
    }


    public String getType() {
        return type;
    }


    public void setType(String type) {
        this.type = type;
    }

    public InputStream getInputStream(){   
             
        return inputStream;  
    }

    public String getExtName() {
        return extName;
    }


    public void setExtName(String extName) {
        this.extName = extName;
    }


    @Override
    public void setServletRequest(HttpServletRequest arg0) {
        // TODO Auto-generated method stub
        this.request=arg0;
    }


    @Override
    public void setServletResponse(HttpServletResponse arg0) {
        // TODO Auto-generated method stub
        this.response=arg0;
    }

    
}
   发表时间:2012-08-13  
你好,请问这个struts.xml怎么配置?还有url怎么写?刚接触这个,多谢啦!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics