我将一些数据从Java发布到PHP:
try {
URL obj = new URL("http://myphpurl/insert.php");
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod(POST_METHOD);
conn.setDoInput(true);
conn.setDoOutput(true);
Map<String, String> params = new HashMap<String, String>();
params.put("title", "العربية");
OutputStream os = conn.getOutputStream();
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
LOG.debug("response {}", response);
in.close();
response = null;
inputLine = null;
conn.disconnect();
conn = null;
obj = null;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
private String getQuery(Map<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();
while (it.hasNext()) {
if (first)
first = false;
else
result.append("&");
Map.Entry<String, String> pairs = it.next();
result.append(URLEncoder.encode(pairs.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pairs.getValue(), "UTF-8"));
it.remove(); // avoids a ConcurrentModificationException
}
return result.toString();
}insert.php文件如下所示:
<?php
$posttitle = $_POST["title"];
echo "$posttitle";
echo urldecode($posttitle);
?>echo显示的是一些粗俗的مليون,而不是实际的标题العربية。
然后将这个gibbrish插入到mysql数据库中。
加法信息:
utf8_general_ci,确实支持阿拉伯语(当我使用phpMyAdmin手动更新帖子时,它是有效的)。InputStreamReader和InputStreamWriter中添加了InputStreamReader和InputStreamWriter,我的行为如下:- Tomcat6 on windows, (PHP + mysql) on CentOS --> OK
- Tomcat6 on CentOS , (PHP + mysql) on CentOS --> Not OK
加性信息2
发布于 2014-10-31 13:31:50
您的代码有很多地方可能出错,我们无法测试它。此外,我建议使用功能齐全的HTTP客户端而不是URLConnection。您应该检查的内容列表:
javac (您的测试是硬编码的)。您是运行相同的二进制程序,还是从IDE运行程序,还是在部署机器上重新编译?)移动部件的数量相当多。您不应该通过print/echo进行调试,因为这会增加另一个级别的转码。如果可能,转储原始文本字节并使用十六进制编辑器。
很有趣的是,是好的,而Linux→Linux则不是。您可能需要检查这两台CentOS机器上的区域设置(可能在目标进程- JVM和Apache内部运行操作系统命令)
发布于 2014-10-31 13:01:35
尝试使用CharsetEncoder显示可能的编码异常。
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);https://stackoverflow.com/questions/26675140
复制相似问题