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;
}
}
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: InputStream按指定编码读取解决乱码问题
- 下一篇: 如何为session设置超时时间?
