牛骨文教育服务平台(让学习变的简单)
博文笔记

inputStream 转为String 类库,不乱码

创建时间:2014-07-19 投稿人: 浏览次数:1898
package com.example.showhtmlorg.Utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {
	
	public static String toString(InputStream is) {
		
		try {
			ByteArrayOutputStream boa=new ByteArrayOutputStream();
			int len=0;
			byte[] buffer=new byte[1024];
			
			while((len=is.read(buffer))!=-1){
				boa.write(buffer,0,len);
			}
			is.close();
			boa.close();
			byte[] result=boa.toByteArray();
			
			String temp=new String(result);
			
//识别编码
			if(temp.contains("utf-8")){
				return new String(result,"utf-8");
			}else if(temp.contains("gb2312")){
				return new String(result,"gb2312");
			}else{
				return new String(result,"utf-8");
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	
	
	

}

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。