我的一些數據後從Java到PHP:POST非拉丁數據到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);
?>
回聲表現出一定的gibbrish مليون
代替的實際標題是العربية
。
然後將這個gibbrish插入到mysql數據庫中。
其它附加信息:
數據庫是
utf8_general_ci
和不支持阿拉伯語(當我手動更新使用phpMyAdmin它的工作原理後)。我在
InputStreamReader
和InputStreamWriter
添加UTF-8
,我有以下行爲:- 的Tomcat6上窗,(PHP + MYSQL)在CentOS - >確定
- 的Tomcat6在CentOS,(在CentOS PHP + MYSQL) - >不正常
其它附加的相關信息2
- 使用javascript進行工作正常:頁面以正確的編碼進行響應。
我從eclipse + tomcat(在windows中)運行它,在CentOS中運行maven打包的war 。 – 2014-10-31 15:26:32
我將contentType MIME類型添加到請求正文,但沒有參數通過了(全爲空):'conn.setRequestProperty(「Content-Type」,「text/plain; charset = utf-8」);' – 2014-10-31 16:03:32
我用javascript來測試相同的php文件http://audiogag.net/preprod_files/submittest.html。它工作得很好,所以它不能成爲PHP的問題。 – 2014-10-31 16:44:57