gpt4 book ai didi

java - 输入流特殊字符UTF-8

转载 作者:行者123 更新时间:2023-12-01 11:20:05 32 4
gpt4 key购买 nike

为什么这个输入流没有返回正确的字符?即使使用 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));

关于java - 输入流特殊字符UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31350852/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com