All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.wslxm.springbootplus2.utils.XjRequestReadUtil Maven / Gradle / Ivy

The newest version!
package io.github.wslxm.springbootplus2.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 *  读取请求数据工具类
 *  @author wangsong
 */
public class XjRequestReadUtil {

    private static final int BUFFER_SIZE = 1024 * 8;

    public static String read(HttpServletRequest request) throws IOException {
        BufferedReader bufferedReader = request.getReader();
        StringWriter writer = new StringWriter();
        write(bufferedReader, writer);
        return writer.getBuffer().toString();
    }

    public static long write(Reader reader, Writer writer) throws IOException {
        return write(reader, writer, BUFFER_SIZE);
    }

    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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy