StreamUtils

来源:互联网 发布:大学生网络防诈骗 ppt 编辑:程序博客网 时间:2024/06/05 14:44
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


public class StreamUtils {



/**
* 将输入流转换为字符串
* @param input
* @return
*/
public static String convertStream2Str(InputStream input){

// 自带缓存的输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte [] buffer = new byte[512];
int len = -1;

try {
// 捕获异常快捷键, 选 中代码,alt +shift +z
while((len = input.read(buffer))!=-1){
baos.write(buffer, 0, len); // 将读到的内容,写入输出流
}

input.close();


return baos.toString();

} catch (IOException e) {
e.printStackTrace();
}

return null;
}
}
0 0
原创粉丝点击