如何在java中处理阿拉伯语

我需要将一串阿拉伯值传递给HttpURL作为参数但在我说我打印消息之前它只显示问号

public void sendSms(SendSms object) throws MalformedURLException,ProtocolException,IOException { String message=new String(object.getMessage().getBytes(), "UTF-8"); System.out.println(message);//printing only question marks } 

甚至当我发送消息作为url参数时,它不发送原始消息作为阿拉伯语发送问号。

 public void sendSms(SendSms object) throws MalformedURLException, ProtocolException,IOException { String message=new String(object.getMessage().getBytes(), "UTF-8"); System.out.println(message); PrintStream out = new PrintStream(System.out, true, "UTF-8"); out.print(message); String charset="UTF-8"; URL url = new URL("http://62.215.226.164/fccsms_P.aspx"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("Accept-Charset", charset); con.setRequestMethod("POST"); //con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en,ar_KW;q=0.5"); con.setRequestProperty("Content-Type", "text/html;charset=utf-8"); String urlParameters = "UID=test&P=test&S=InfoText&G=965"+object.getPhone()+"&M= Hello "+object.getName()+" "+message+" &L=A"; // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } 

假设SendSms.getMessage()返回一个String ,你打算用这行做什么?

 String message = new String(object.getMessage().getBytes(), "UTF-8"); 

对消息进行编码和解码是最好的浪费 – 如果它有效,你只需要找回你开始使用的字符串。 它只有在默认编码为UTF-8时才有效。 在这种情况下,它破坏了消息。

使用getBytes()编码String时,将使用平台默认编码。 除非系统的本机字符集支持阿拉伯语,否则任何阿拉伯字符都会被支持的字符替换,通常?

试试这个:

 String message = object.getMessage(); 

至少,你需要遵循erickson的回答中的建议。 如果仍有问题,请继续阅读……

Windows命令窗口在可以显示的字符方面非常有限。 我不知道各种语言包和代码页如何影响它,但我知道Windows的英文版将显示阿拉伯字符(以及U + 00FF以上的大多数字符)为?

所以你的角色完全可能是正确的,它们只是被展示出来了? 打印时

而不是使用System.out.println,使用记录器 。 然后,您可以将处理程序添加到写入文件的 Logger(或根Logger),您可以检查该文件:

 private static final Logger logger = Logger.getLogger(SmsSender.class.getName()); // ... public void sendSms(SendSms object) throws IOException { String message = new String(object.getMessage().getBytes(), StandardCharsets.UTF_8); logger.info(() -> "Message=\"" + message + "\""); // ... int responseCode = con.getResponseCode(); logger.info(() -> "Sending 'POST' request to URL : " + url); logger.info(() -> "Post parameters : " + urlParameters); logger.info(() -> "Response Code : " + responseCode); 

(顺便说一句,MalformedURLException和ProtocolException都是IOException的子类,所以你只需要在方法签名中throws IOException 。它本身就涵盖了另外两个例外。)

在其他一些课程中,大概是在你的main方法中:

 int logFileMaxSize = 10 * 1024 * 1024; // 10 MB Logger.getLogger("").addHandler( new FileHandler("%h/Sms-%g.log", logFileMaxSize, 10));