且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用UTF-8读取InputStream?

更新时间:2023-11-27 12:49:28

当您获取InputStream时,请从中读取byte[].创建字符串时,请在CharSet中传递"UTF-8".示例:

When you get your InputStream read byte[]s from it. When you create your Strings, pass in the CharSetfor "UTF-8". Example:

byte[] buffer = new byte[contentLength];
int bytesRead = inputStream.read(buffer);
String page = new String(buffer, 0, bytesRead, "UTF-8");

请注意,您可能需要使缓冲区达到合理的大小(例如1024),并连续调用inputStream.read(buffer).

Note, you're probably going to want to make your buffer some sane size (like 1024), and continuously called inputStream.read(buffer).

@Amir Pashazadeh

@Amir Pashazadeh

是的,您还可以使用InputStreamReader,然后尝试将parse()行更改为:

Yes, you can also use an InputStreamReader, and try changing the parse() line to:

Document doc = db.parse(new InputSource(new InputStreamReader(in, "UTF-8")));