---
title: "数据爬取 -- 编码（UTF-8 to GBK）"
author: "seabay"
date: "2012-07-11T17:18:24Z"
tags: []
categories: ["互联网"]
slug: "utf-8-to-gbk"
draft: false
lastmod: "2012-07-11T17:18:24Z"
id: 1585207
url: "https://seabay.iteye.com/blog/1585207"
---

昨日从某点评网爬取数据

网页编码为 UTF-8, 本地的系统默认编码为 GBK

直接使用 BufferReader 读取 HttpURLConnection 打开的 stream，会导致乱码

使用 DataInputStream 的 readUTF 也引起乱码

最后使用如下代码，得到正确的文本，如下：

```
BufferedReader dis;

String content = new String();
String line;
try {

    dis = new BufferedReader(new InputStreamReader(is, "UTF-8"));

    while ((line = dis.readLine()) != null) {
        content += line;
    }
    dis.close();
    String nct = new String(content.getBytes("GBK"));
    } catch (Exception e) {
        e.printStackTrace();
}
```