Apache Tomcat v8.5不支持阿拉伯语



我有一个移动应用程序,它的rest Api是使用eclipse在Jakarta EE中编写的,数据库是oracle,当然,我们将war文件上传到apache服务器上用于实时应用程序,我们使用的apache服务器版本是tomcat apache v8.5。当我们通过应用程序插入数据,同时使用我们的本地服务器,然后阿拉伯语文本工作良好,并正确地出现在oracle。但是当我们上传war文件并通过tomcat apache使用live应用程序时,阿拉伯语文本就不起作用了。我使用了几个选项,但没有得到正确的结果。我还尝试设置URIEncoding="UTF-8"在server.xml文件中的连接器内部,但它不起作用。

<?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 &quot;%r&quot; %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类rest Api代码

按照@PiotrP的建议,我使用了两个参数的InputStreamReader构造函数。Karwasz和它的工作

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();
}