inputStream 转为String 类库,不乱码
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;
}
}
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
