现在的位置: 首页 > 综合 > 正文

将输入流转换为字符串

2014年02月15日 ⁄ 综合 ⁄ 共 491字 ⁄ 字号 评论关闭

如果要输入流转换为字符串使用ByteArrayOutputStream来实现:

private static String changeInputStream(InputStream inputStream,
			String encode) {
		// TODO Auto-generated method stub
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = 0;
		String result = "";
		if (inputStream != null) {
			try {
				while ((len = inputStream.read(data)) != -1) {
					outputStream.write(data, 0, len);
				}
				result = new String(outputStream.toByteArray(), encode);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

抱歉!评论已关闭.