欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

InputStream2String

程序员文章站 2024-02-04 08:22:40
...

public static String InputStream2String(InputStream in) {
		return InputStream2String(in, null);
	}
	public static String InputStream2String(InputStream in,String charset) {
		if (charset == null){
			charset = "UTF-8";
		}
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		String result = null;
		int bufferSize = 2048;
		byte[] data = new byte[bufferSize];
		int count = -1;
		try {
			while ((count = in.read(data, 0, bufferSize)) != -1) {
				outputStream.write(data, 0, count);
			}
			result = new String(outputStream.toByteArray(), charset);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
 
相关标签: InputStream String