有的时候html文件的编码方式与meta信息中指定的编码方式不同,可以通过这段代码搞一下。 此程序依赖jsoup和commons-io包
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.Iterator;
-
- import org.apache.commons.io.FileUtils;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
-
- public class main {
-
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
-
- File input = new File("C:\Users\jack\Desktop\新建文件夹\jdk-zh");
- Iterator it = FileUtils.iterateFiles(input, null, true);
- while (it.hasNext()) {
- File file = it.next();
- Document doc = Jsoup.parse(file, "gb2312");
- Elements content = doc.getElementsByAttributeValueStarting("content", "text/html;");
- for (Element meta : content) {
- meta.attr("content", "text/html; charset=utf-8");
- System.out
- .println("修改content--------" + file.getName() + "---");
- }
- FileUtils.writeStringToFile(file, doc.html(),"utf-8");
- }
- }
- }
-
复制代码
|