我在我创建的IBM WebSphere Application中遇到了Cyrillic符号表示问题(它们看起来像' 0B0; lO')。
所以我使用WebSphere 8.5(Unix Red Hat),而application是一个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或只是响应时,这些符号的视图都是错误的。
请帮助解决问题!
答案 0 :(得分:0)
此问题已解决。
请求正确处理了西里尔符号,问题与响应有关。以下代码有帮助。
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();