问题:highcharts导出图片的功能每次都要经过远程的服务器。
解决:使用自己的写的action或者servlet来实现这个下载图片的功能。不需要连接外网
1.汉化highcharts:
Highcharts.setOptions({ lang: { printChart:"打印图表", downloadJPEG: "下载JPEG 图片" , downloadPDF: "下载PDF文档" , downloadPNG: "下载PNG 图片" , downloadSVG: "下载SVG 矢量图" , exportButtonTitle: "导出图片" } }); 2.在初始化图形时添加一个属性: Highcharts.chart('container', { chart : { type : 'pie', options3d : { enabled : true, alpha : 45, beta : 0 } }, exporting:{ enabled:true, filename:'class-booking-chart', url:'<%=basePath%>exportAction!export.action', }}) 3.这样图片的请求就切换到本地了。我这里用的是注解的struts2,mvc框架用法都一样 package com.libo.yy.action; import java.io.StringReader; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.batik.transcoder.SVGAbstractTranscoder; 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.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import com.libo.action.util.BaseAction; import com.libo.po.User; @Action(value = "exportAction") public class HighcharsExportAction extends BaseAction<User> { private String type; private String svg; public void export() { try { HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); request.setCharacterEncoding("utf-8");// 注意编码 ServletOutputStream out = response.getOutputStream(); if (null != type && null != svg) { svg = svg.replaceAll(":rect", "rect"); String ext = ""; SVGAbstractTranscoder 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); t.transcode(input, output); } } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getSvg() { return svg; } public void setSvg(String svg) { this.svg = svg; } } 4.这里需要注意的是需要导入batik的jar包在这里引用一个下载频道中的一个连接点击打开链接