常用封装 IOUtils

原创
2020/12/24 20:53
阅读数 67

常用来读取 request 中 body 的内容 

public class IOUtils {

	public static String read(Reader reader) throws IOException {
		try (StringWriter writer = new StringWriter()) {
			write(reader, writer, 8 * 1024);
            return writer.getBuffer().toString();
        }
	}
	
	public static long write(Reader reader, Writer writer, int bufferSize) throws IOException {
		int read;
		long total = 0;
		char[] buf = new char[bufferSize];
		while ((read = reader.read(buf)) != -1) {
			writer.write(buf, 0, read);
			total += read;
		}
		return total;
	}
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部