Highchart using servlet to export Chinese PNG image garbled problem solving

Source: Internet
Author: User

Recently used to Highchart drawing, in the picture export, appeared in the picture of Chinese garbled problem, in the network to find a lot of information, but did not solve, finally found the most easily overlooked problems. See below for details.

Because of previous colleagues used Highchart, so do not hesitate to use the method before colleagues: through their own writing servlet, the use of batik tools to complete the export of different formats, the specific method see below:

@RequestMapping (value = "/save_image", method = requestmethod.post) public void SaveImage (HttpServletRequest request, HttpServletResponse response, String type, string svg, string filename) throws IOException {R        Equest.setcharacterencoding ("Utf-8"); filename = filename = = null?        "Chart": filename;        Servletoutputstream out = Response.getoutputstream ();            if (null! = Type && null! = SVG) {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 = (transcoder) new Pdftranscoder (); } else if (Type.equals ("Image/svg+xml")) ext = "SVG"; Response.AddHeader ("Content-disposition", "attachment;            Filename= "+ filename +". "+ 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. ");}}                else if (ext.equals ("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 is converted.        + "\n\tparameter [Type]: The destination MIME type for the elment to be transcoded.");        } out.flush ();    Out.close (); }

Then modify the Exporting.js file to change its export URL to the URL specified in the servlet above. method is roughly the same as on the network.

But the problem came, the test colleagues told me that the application deployed on the Linux server when the export of pictures, Chinese has become a small box, generally this garbled problem will think of coding problems, and began to find a solution on the network.

First, based on the workaround two mentioned in http://ollevere.iteye.com/blog/1773563, the code adds a

Request.setcharacterencoding ("Utf-8");

Discovery does not work after you deploy the test

So, continue the sea search, see the http://jstree.iteye.com/blog/1586623, and add in the code

Response.setcharacterencoding ("Utf-8");

After you deploy the test, you find that it doesn't work.

In fact, I am in the servlet configuration of the project, the font has been done transcoding configuration. If you are not configuring, you should control the encoding in the servlet as described above.

<mvc:annotation-driven> <mvc:message-converters register-defaults= "true" > <!--will Stringhtt The default encoding for Pmessageconverter is set to UTF-8---<beanclass= "Org.springframework.http.converter.StringHttpMessageConverter" > <constructor-arg value= "UTF-8"/&gt            ; </bean> <!--Set the default formatted output of Jackson2httpmessageconverter to true--<Beanclass= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > <property name= "Prett                        Yprint "value=" true "/> <property name=" Supportedmediatypes "> <list> <value>text/html;charset=UTF-8</value> </list> < /property> </bean> </mvc:message-converters> </mvc:annotation-driven>

At this time I test found that the application deployed on the local (WINDOWS7 system) Tomcat server does not have this problem, the exported image in Chinese is very normal, then, a little confused.

So I think there is not a Linux server where the correct configuration? But after comparing the configuration of the local tomcat and the server tomcat, we found that the two were much the same, even though I copied the local configuration to the server, and after the test was deployed, the problem persisted. Think of the Java version will be different, the results, the Linux server is used Java1.7, and the local use is Java1.6. But there was no problem in looking at it.

At this time, I feel very powerless, also almost delayed a day.

So began to look for colleagues to consult, colleagues looked after the mention is not a Linux server is not supported by the set of Chinese fonts, suddenly feel a bright, may find the problem, so the front-end colleagues with the modification of Highchart paint set font-family, Added the default font monospace in Linux, but the problem is still unresolved.

After looking for a system colleague Consulting: is the Linux system lacks the Chinese font package or the JDK is missing the Chinese font package to cause this problem, he gave the answer is that the JDK is missing the Chinese font package, so, in the JDK installed a few common Chinese font package, and then deploy the application test, The problem is solved . Although previously thought may be the server configuration problem, but did not expect the JDK is missing the Chinese font package, or inexperienced AH.

The error-checking process is very tortuous, but finally solved the problem, deliberately sorted out for your reference. There are shortcomings, please forgive us.

Highchart using servlet to export Chinese PNG image garbled problem solving

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.