开发者

getting junk characters in firefox where as working fine in IE

开发者 https://www.devze.com 2023-01-30 01:11 出处:网络
I have used the below piece of code to get the output on the browser using printwriter. String b= new String(a.getBytes(\"UTF-16LE\"),\"UTF-8\");

I have used the below piece of code to get the output on the browser using printwriter.

String b= new String(a.getBytes("UTF-16LE"),"UTF-8"); 

The output is fine with IE but in firefox i get

<�h�3�>�C�o�m�m�e�n�t�s�<�/�h�3�>� �<�t�a�b�l�e�>�<�t�r� �b�g�c�o�l�o�r�=�'�#�E�7�E�7�E�F�'�>�<�t�d�>�P�o�s�t�e�d� �O�n�:� �1�2�-�1�3�开发者_运维技巧-�2�0�1�0� �1�0�:�3�8�:�2�4� �,� �B�y�:

This is the sample output of the problem facing.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "w3.org/TR/html4/loose.dtd">;
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" 
      CONTENT="no-cache">
  </head> 
  <body>
    <form name="form" method="post">
    <% 
    String theString = null; 
    PrintWriter pw = null; 
    String cmntbox = ""; 
    ServletOutputStream outStream = null; 
    try { 
      Connection con = null; 
      PreparedStatement pstmt = null; 
      ResultSet rs = null; 
      String sql = null; 
      InputStream sImage; 
      con = DBConnection.getConnection();
      sql = SELECT statement 
      pstmt = con.prepareStatement(sql); 
      rs = pstmt.executeQuery(); 
      while (rs.next()) { 
        byte bytearray[] = new byte[1048576]; 
        String newLine = System.getProperty("line.separator"); 
        sImage = rs.getBinaryStream(1); 
        StringWriter writer = new StringWriter(); 
        request.setCharacterEncoding("UTF-8"); 
        response.setContentType("text/html;charset=UTF-8"); 
        pw =response.getWriter(); 
        IOUtils.copy(sImage, writer); 
        theString = writer.toString(); 
        pw.write(theString); 
        pw.flush();
        cmntbox = Utility.getCommentPage(id,prcs_area); 
        String comment = new String(cmntbox.getBytes("UTF-16LE"),"UTF-8"); 
        pw.write(comment); 
        pw.flush(); 
        pw.close(); 
      }


String comment = new String(cmntbox.getBytes("UTF-16LE"),"UTF-8");

is almost certainly wrong. The String that you get from Utility.getCommentPage should contain the characters you want. Converting those characters into bytes using one encoding, and then converting the bytes back into characters with another encoding, would result in what you're seeing.

In particular, every other byte in UTF-16 encoding for typical HTML is zero. And zero is a perfectly valid byte that encodes into a single NUL character in UTF-8. IE might be thinking, "Clearly this is wrong, so I will try to be helpful and do what I think you meant"; while Firefox is just showing what you said you wanted.

What happens if you skip the conversion and just do

pw.write(cmntbox);

directly? You already did the response.setContentType with the charset= before you did the response.getWriter. When you then write a String, it should do the character encoding for you.


I think your problem is that firefox doesn't know you are trying to display unicode. Have you tried putting in a proper doctype and content type?

The content type should be < meta http-equiv="Content-Type" content="text/html; charset=utf-8" >

Similar issue - http://www.webdeveloper.com/forum/showpost.php?s=7981302b7ad7c59bb2b79ff03b462e45&p=680562&postcount=6

0

精彩评论

暂无评论...
验证码 换一张
取 消