java - Apache Tomcat v8.5 不支持阿拉伯语
问题描述
我有一个移动应用程序,它的其余 Api 是使用 eclipse 在 Jakarta EE 中编写的,数据库是一个 oracle,当然,我们将 war 文件上传到 apache 服务器以进行实时应用程序,我们使用的 apache 服务器版本是 tomcat阿帕奇 v8.5。当我们在使用本地服务器时通过应用程序插入数据时,阿拉伯语文本可以正常工作并正确显示在 oracle 中。但是当我们通过 tomcat apache 上传战争文件并使用实时应用程序时,阿拉伯语文本不起作用。我使用了几个选项,但没有正确获得结果。我还尝试在 server.xml 文件的连接器内设置 URIEncoding="UTF-8",但它不起作用。
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
--><Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
<Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector connectionTimeout="20000" port="8080" maxHttpHeaderSize="65536" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
<Context docBase="JobVisit" path="/JobVisit" reloadable="true" source="org.eclipse.jst.jee.server:JobVisit"/></Host>
</Engine>
</Service>
</Server>
但是当我尝试在 Eclipse 中将我的字符串转换为 UTF-8 时,它会以某种方式工作,但由于“ف”显示为“??”,因此某些字母不正确。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.google.gson.Gson;
import com.rest.database.DBConnection;
@Path("/arabicTest")
public class MaintenanceRequestRegistrationTest {
@SuppressWarnings({ "unchecked" })
@POST
@Path("arabic")
@Produces("application/json")
@Consumes("application/json")
public String crunchifycreateKCM(InputStream incomingData) throws JSONException {
Connection dbConnection = null;
Statement stmt = null;
// JSONArray myArray = new JSONArray();
JSONObject jsarr = new JSONObject();
try {
// con.setAutoCommit(false);
dbConnection = DBConnection.getDBConnection();
dbConnection.setAutoCommit(false);
stmt = (Statement) dbConnection.createStatement();
// System.out.println("Iam entering try catch bloxck");
new Gson();
InputStream in = incomingData;
String a = convertStreamToString(in);
// System.out.println(a);
JSONObject json = new JSONObject(a);
JSONArray kcm = json.getJSONArray("createkcm");
// System.out.println("length of json data"+kcm.length());
// updating kcmd data
JSONObject jo = kcm.getJSONObject(0);
String cmremarks = jo.getString("cmremarks");
cmremarks = new String(cmremarks.getBytes(),"UTF-8");
String sqlQuery3 = "insert into test(COL1) values('"+ cmremarks + "')";
int count = stmt.executeUpdate(sqlQuery3);
if(count>0) {
jsarr.put("status", "200");
}else {
jsarr.put("status", "100");
}
dbConnection.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
// db connection close 16-Nov
try {
if (stmt != null)
stmt.close();
if (dbConnection != null)
dbConnection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// return HTTP response 200 in case of success
// return Response.status(200).entity(ret.toString()).build();
System.out.print(jsarr.toString());
return jsarr.toString();
}
private String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means there's
* no more data to read. Each line will appended to a StringBuilder and returned
* as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
所以请任何人都可以指导我如何解决这个问题,我还附上了我的 server.xml 和 java 类休息 Api 代码
解决方案
根据@PiotrP.Karwasz 的建议,我使用了两参数 InputStreamReader 构造函数,它可以工作
private String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means there's
* no more data to read. Each line will appended to a StringBuilder and returned
* as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
推荐阅读
- homebrew - brew 搜索显示时无法安装 go@1.13
- spring-boot - 将现有项目添加到已有文件的 github 存储库
- matlab - 曲线的旋转、曲线的交点和for循环的应用【浮点比较问题】】
- macos - 工具箱不适用于 Visual Studio for Mac
- javascript - nodejs中日期构造函数中使用的时间格式是什么?
- docker - 如何知道 docker 镜像何时被拉入我的 docker 守护进程
- python - venv 没有在 python/windows 上跨越 subprocess.run?
- node.js - Mongoose:OverwriteModelError:编译后无法覆盖“用户”模型
- python - TypeError: start() 接受 0 个位置参数,但给出了 1 个
- python - Tensorflow - 训练大小和训练值之间存在差异的原因是什么