[英]InputStream special character UTF-8
為什么此輸入流未返回正確的字符? 即使使用UTF-8讀取后,Código仍會變成Cédigo
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setRequestProperty("Accept-Charset", "UTF-8");
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
byte[] buffer = new byte[1024];
int bytesRead = stream.read(buffer);
String page = new String(buffer, 0, bytesRead, "UTF-8");
//Page is returning, C�digo instead of Código
嘗試這個,
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
解決方案是:
//set the conn to this
conn.setRequestProperty("Accept-Charset", "ISO-8859-1");
//use Apache Commons IO
data = IOUtils.toString(stream, "ISO-8859-1");
並在.php頁面中輸入:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.