---
title: "UTF-8 的bom格式处理"
author: "yrandy"
date: "2012-09-14T11:54:38Z"
tags: []
categories: ["Web前端"]
slug: "utf-8-bom"
draft: false
lastmod: "2012-09-14T11:54:38Z"
id: 1678383
url: "https://yrandy.iteye.com/blog/1678383"
---

**一般通过httpcient请求的内容，有可能返回utf-8带有bom格式的文件，这样对于我们处理字符串内容就会有影响，需要去除bom的头标示字节。下面是代码：**

            String st="utf-8的bom格式字符串内容";

            byte\[] bytes = st.getBytes("utf-8");

    byte\[] strs = new byte\[bytes.length - 3];//去掉该内容的bom标示

    int j = 0;

    if (bytes\[0] == -17 && bytes\[1] == -69 && bytes\[2] == -65) {    //bom文件格式头字节

for (int i = 3; i &lt; bytes.length; i++) {

strs\[j] = bytes\[i];

j++;

      }