Weblogic containers output images through JSP pages without garbled characters, but Tomcat works normally.

Source: Internet
Author: User

These days, I have been dizzy with a problem. In my project, I want to implement such a function to display a three-dimensional vector chart in the report (which is actually not so amazing ), therefore, you need to write a jsp page to output the image.

It was already successfully debugged under tomcat, but garbled characters appeared when the on-site debugging function was reached. As a result, I tried my best to solve the problem. Finally, under the guidance of the company, we finally solved the problem.

The file processing rules of weblogic are different from those of tomcat, as shown in the following code:


 

<%@page import="java.io.OutputStream"%> <%@page import="javax.imageio.ImageIO"%> <%@page import="java.awt.Color"%> <%@page import="java.awt.Font"%> <%@page import="java.awt.Graphics"%> <%@page import="java.awt.image.BufferedImage"%> <%@page import="java.io.BufferedOutputStream"%> <%@page import="java.io.FileOutputStream"%> <%@page import="java.util.Date"%> <%@page import="java.util.Random"%> <%@page import="com.sun.image.codec.jpeg.JPEGCodec"%> <%@page import="com.sun.image.codec.jpeg.JPEGImageEncoder"%> <%@page import="java.io.OutputStream"%><%@page import="javax.imageio.ImageIO"%><%@page import="java.awt.Color"%><%@page import="java.awt.Font"%><%@page import="java.awt.Graphics"%><%@page import="java.awt.image.BufferedImage"%><%@page import="java.io.BufferedOutputStream"%><%@page import="java.io.FileOutputStream"%><%@page import="java.util.Date"%><%@page import="java.util.Random"%><%@page import="com.sun.image.codec.jpeg.JPEGCodec"%><%@page import="com.sun.image.codec.jpeg.JPEGImageEncoder"%>

For tomcat containers, tomcat will automatically process
 

。[<%@page import="java.io.OutputStream"%><%@page import="javax.imageio.ImageIO"%> <%@page import="java.io.OutputStream"%><%@page import="javax.imageio.ImageIO"%>


That is to say, for jsp files, tomcat automatically removes line breaks and space characters.

However, weblogic does not have such a processing rule. Instead

 

<%@page import="java.io.OutputStream"%> <%@page import="javax.imageio.ImageIO"%> <%@page import="java.io.OutputStream"%><%@page import="javax.imageio.ImageIO"%>

Such code is output with a carriage return. This symbol is naturally combined with the stream of the output image below. Some line breaks are added at the beginning of the image stream, which naturally cannot be displayed normally.

But garbled characters appear.

The key to solving this problem is to check whether there are redundant carriage returns and spaces in the jsp file. If yes, remove them, then, change the declaration at the beginning of the file to the following form to avoid outputting redundant carriage returns.

The correct jsp page should be like this, for example:

<% @ Page import = "java. io. outputStream "%> <% @ page import =" javax. imageio. imageIO "%> <% @ page import =" java. awt. color "%> <% @ page import =" java. awt. font "%> <% @ page import =" java. awt. graphics "%> <% @ page import =" java. awt. image. bufferedImage "%> <% @ page import =" java. io. bufferedOutputStream "%> <% @ page import =" java. io. fileOutputStream "%> <% @ page import =" java. util. date "%> <% @ page import =" java. util. random "%> <% @ pag E import = "com.sun.image.codec.jpeg. sharecodec" %> <% @ page import = "com.sun.image.codec.jpeg. shareimageencoder" %> <%! /*** Add text **/private void addText (Graphics graphics, String [] zbm, int I, Integer [] bd, int wz ){/*... Privacy... */}%> <% Try {String zfx = request. getParameter ("zfx"); String i1 = request. getParameter ("uaia"); String i2 = request. getParameter ("ubib"); String i3 = request. getParameter ("ucic"); Double uaia = Double. valueOf (i1); Double ubib = Double. valueOf (i2); Double ucic = Double. valueOf (i3); int imageWidth = 224; // The image Width int imageHeight = 175; // The image Height BufferedImage image = new BufferedImage (imageWidth, imageHeight, BufferedImage. TYPE_INT_RGB); Graphics graphics = image. getGraphics (); graphics. setColor (Color. white); graphics. fillRect (0, 0, imageWidth, imageHeight); graphics. setColor (Color. black);/* privacy */graphics. dispose (); OutputStream outs = response. getOutputStream (); ImageIO. write (image, "jpeg", outs);} catch (Exception e) {e. printStackTrace () ;}%>

 

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.