读书人

ByteArrayOutputStream 转为 String 出

发布时间: 2012-08-30 09:55:54 作者: rapoo

ByteArrayOutputStream 转为 String 出现的乱码问题

??????????????????????????? InputStream is = conn.getInputStream();

?????????? ByteArrayOutputStream baos = new ByteArrayOutputStream();

??????????

?????????? byte[] buffer = new byte[1024];

?????????? int len = -1;

??? ?????? while((len = is.read(buffer)) != -1){

????????????? baos.write(buffer , 0 , len);

?????????? }

?????????? baos.close();

?????????? is.close();

?????????????

?????????? String result = new String( baos.toByteArray());

?????? ??? /*

?????????? * 到此步骤时:Debug-Watch 查看值:

?????????? *????? 1.baos对象中含有中文? 无乱码;

?????????? *????? 2.result 对象中含有中文? 出现乱码!

?????????? */

?

?????????? //解决此问题办法:

?????????? byte[] lens = baos.toByteArray();

?????????? String result = new String(lens);//result结果显示正常:含中文无乱码

读书人网 >编程

热点推荐