Jsoup 中的字符集
Charset in Jsoup
我使用 Jsoup 库。
After the execution of the following code:
Document doc = new Document(language);
File input = new File("filePath" + "filename.html");
PrintWriter writer = new PrintWriter(input, "UTF-8");
String contentType = "<%@ page contentType=\"text/html; charset=UTF-8\" %>";
doc.appendText(contentType);
writer.write(doc.toString());
writer.flush();
writer.close();
In the output html file I receive the following line of text:
<%@ page contentType="text/html; charset=UTF-8" %>
而不是
<%@ page contentType="text/html; charset=UTF-8" %>
可能是什么问题?
这些是转义字符,用于防止浏览器将它们视为 html 标记。这不是问题。当您通过浏览器打开页面时,它将正确呈现
这里有些问题:
Document doc = new Document(language);
不要这样做。请改用 Jsoup.parse(...)
。
<%@ page contentType="text/html; charset=UTF-8" %>
这不是 HTML,可能无法正确解析。
现在,针对您的问题。你应该使用类似
Document document = Jsoup.parse(new ByteArrayInputStream(myHtmlString.getBytes(StandardCharsets.UTF_8)), "ISO-8859-1", BaseUrl);
检查 this, this, and this 您可能需要的输出设置。
我使用 Jsoup 库。
After the execution of the following code:
Document doc = new Document(language);
File input = new File("filePath" + "filename.html");
PrintWriter writer = new PrintWriter(input, "UTF-8");
String contentType = "<%@ page contentType=\"text/html; charset=UTF-8\" %>";
doc.appendText(contentType);
writer.write(doc.toString());
writer.flush();
writer.close();
In the output html file I receive the following line of text:
<%@ page contentType="text/html; charset=UTF-8" %>
而不是
<%@ page contentType="text/html; charset=UTF-8" %>
可能是什么问题?
这些是转义字符,用于防止浏览器将它们视为 html 标记。这不是问题。当您通过浏览器打开页面时,它将正确呈现
这里有些问题:
Document doc = new Document(language);
不要这样做。请改用 Jsoup.parse(...)
。
<%@ page contentType="text/html; charset=UTF-8" %>
这不是 HTML,可能无法正确解析。
现在,针对您的问题。你应该使用类似
Document document = Jsoup.parse(new ByteArrayInputStream(myHtmlString.getBytes(StandardCharsets.UTF_8)), "ISO-8859-1", BaseUrl);
检查 this, this, and this 您可能需要的输出设置。