该文是解决highchart导出中文乱码的笔记。
步骤:
1、下载jar包
我下了5个,网上说可以下载4个
2.修改web.xml
3.新建servlet
4.将highchats 的exporting属性的url修改为 自己建立的servlet
具体如下:
2.web.xml中增加如下代码
<
servlet
>
<
servlet-name
>
HighChartsServlet
servlet-name
>
<
servlet-class
>
com.sinosoft.lis.highcharts.HighChartsServlet
servlet-class
>
<
load-on-startup
>
10
load-on-startup
>
servlet
>
<
servlet-mapping
>
<
servlet-name
>
HighChartsServlet
servlet-name
>
<
url-pattern
>
/getHighchartServlet
url-pattern
>
servlet-mapping
>
3.sevlet代码如下
package
com.sinosoft.lis.highcharts;
import
java.io.IOException;
import
java.io.StringReader;
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.p_w_picpath.JPEGTranscoder;
import
org.apache.batik.transcoder.p_w_picpath.PNGTranscoder;
import
org.apache.fop.svg.PDFTranscoder;
/**
* Servlet implementation class for the batik Transcoder
*
@author
tangyujing
* 2013
-
09
-
06上午03:54:39
*/
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"
);
//注意编码
String type = request.getParameter(
"type"
);
String svg = request.getParameter(
"svg"
);
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(
"p_w_picpath/png"
)) {
ext =
"png"
;
t =
new
PNGTranscoder();
}
else
if
(type.equals(
"p_w_picpath/jpeg"
)) {
ext =
"jpg"
;
t =
new
JPEGTranscoder();
}
else
if
(type.equals(
"application/pdf"
)) {
ext =
"pdf"
;
t =
new
PDFTranscoder();
}
else
if
(type.equals(
"p_w_picpath/svg+xml"
)) {
ext =
"svg"
;
}
response.addHeader(
"Content-Disposition"
,
"p_w_upload; 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);
}
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();
}
}
4.
jsp中highchs的导出功能
//增加导出功能
exporting: {
enabled:
true
,
///url:getRootPath()+"/getHighchartServlet",
url:
'http://localhost:8080/xiangmuming/getHighchartServlet'
,
filename:
'chart'
,
width:800,
type:
'p_w_picpath/png'
},
或者采用(这是设置时间方法,下载pdf)
chart.exportChart({
url:
'http://localhost:8080/hr821/getHighchartServlet'
,
type:
'application/pdf'
,
filename:
'fff'
});
//导出pdf