【问题标题】:Character encoding in web applicaion running on Websphere Application Server 8.5在 Websphere Application Server 8.5 上运行的 Web 应用程序中的字符编码
【发布时间】:2015-02-09 09:11:54
【问题描述】:

我在我创建的 IBM WebSphere Application 中的西里尔符号表示存在问题(它们看起来像 '0B0;lO')。

所以我使用 WebSphere 8.5 (Unix Red Hat),应用程序是一个 java-servlet,它监听 HTTP POST 请求。

WebSphere 上的 JVM 属性是:

  • 默认字符集=UTF-8

  • 默认字符集=使用中=UTF-8

  • file.encoding=UTF-8

我检查了文件 encoding.properties 并将编码设置为 UTF8。我通过以下代码以 UTF8 格式向我的应用程序发送请求:

   HttpURLConnection connection = (HttpURLConnection)url.openConnection();
   connection.setRequestMethod("POST"); 
   connection.setRequestProperty("Accept-Charset", "UTF-8");
   connection.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
   connection.setRequestProperty("Content-Length", Integer.toString(resultXML.getBytes().length));
   connection.setUseCaches (false);
   connection.setDoInput(true);
   connection.setDoOutput(true);

   //Send request
   DataOutputStream wr = new DataOutputStream (
             connection.getOutputStream ());
  // BufferedWriter br = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
   BufferedWriter br1 = new BufferedWriter(new OutputStreamWriter(wr, Charset.forName("UTF-8")));
   br1.write(resultXML);
   br1.close();
   wr.flush ();
   wr.close ();

   //Get Response
   BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
   String line;
   while ((line = rd.readLine()) != null) {
      System.out.println(line);
   }
   rd.close();

在应用程序 HTTP 请求处理程序中,我首先通过以下方式设置编码:

        response.setCharacterEncoding("UTF-8");

        request.setCharacterEncoding("UTF-8");

但是请求和响应中的所有西里尔符号看起来都不正确,当应用程序将它们放入 Oracle DB 或只是为了响应这些符号时,这些符号的视图有误。

请帮忙解决问题!

【问题讨论】:

  • 你如何阅读你的xml?

标签: java encoding utf-8 character-encoding websphere


【解决方案1】:

此问题已解决。

请求西里尔符号已正确处理,问题在于响应。以下代码有所帮助。

        PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
        for (int i = 0; i < result.length(); i++)
        {
            out.print(result.charAt(i));
        }

        out.flush();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多