1 Reply Latest reply: Jul 27, 2012 1:25 PM by 947708 RSS

    Weblogic 12c Servlet Response - Special characters show up as question mark

    947708
      My web app is running on Weblogic 12c (12.1.1) using WebWork + Hibernate. The program streams data (bytes making up a pdf) from a CLOB in an Oracle Database to the AsciiStream of the servlet output response. No exceptions are thrown, but the generated pdf contains blank pages. Comparing the bytes of the generated pdf, special characters are showing up as question marks.

      Some of the bytes read in from the database contain 8 bits (correct data), but the bytes that the servlet return contain only 7 (all bytes with 8 bits become "1111111"). The number of bytes returned from the servlet is correct.

      -----

      Code:

      //Response is HttpServletResponse

      response.setContentType("application/pdf");
      response.setHeader("Content-Disposition", "inline; filename=\"test.pdf\"");

      out = response.getOutputStream();

      byte[] buf = new byte[16 * 1024];

      InputStream in = clob.getAsciiStream();

      int size = -1;
      while ((size = in.read(buf)) != -1){
      // buf contains the correct data
      out.write(buf, 0, size);
      }
      // other exception handling code, etc

      out.flush();
      out.close();

      -----

      "Correct" pdf byte example:
      10011100
      10011101
      1010111
      1001011
      1101111
      11011011

      Incorrect pdf byte example:
      111111
      111111
      1010111
      1001011
      1101111
      111111

      I have verified that the data read from the CLOB in the database IS correct. My guess is that the Weblogic server has some strange servlet settings that causes the bytes to be written to the servlet output stream incorrectly, or a character encoding issue. Any ideas?

      Edited by: 944705 on Jul 26, 2012 10:17 AM
        • 1. Re: Weblogic 12c Servlet Response - Special characters show up as question mark
          947708
          Solution found, I'll post the work around to those who might encounter the same problem.

          Somewhere in the layers of technology (webwork or weblogic I'd guess), the servlet response is encoded into UTF-8 regardless. The encoding in the database was ISO-8859-1. Sending ISO encoded bytes by UTF-8 caused the conflicting character codes (anything above 127) to show up as undefined.

          The fix is to decode the input byte array into ISO-8859 string, then encode that string into UTF-8, which can be send by Weblogic.


          isoConvert = new String(buf, "ISO-8859-1");
          out.write(isoConvert.getBytes("UTF-8"), 0, isoConvert.getBytes("UTF-8").length);