getInputStream() has already been called for this request解决方法二

HttpServletRequest读取流的时候,出现如标题所示无法再读或者读出来表现为空的情况,是由于在框架或者其他地方已经读了一次。

上一篇中用了一种方式处理;在上一种方式中,把HttpServletRequest全部实例化为MyRequestWrapper,用来保存副本;个人始终觉得不是很好。

今晚回家,思考了一下,用另外一种方式进行了处理,最终效果一致;记录如下:

package net.dlet.dhdemo.configure;

import net.dlet.dhdemo.utils.dhutils.CarHttpServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 自定义servlet配置类
 * @PackageName: net.dlet.dhdemo.configure
 * @Name: CarServletConfig
 * @Author: crue
 * @CreateDate: 2020/09/07 21:24
 * @DayNameFull: 星期一
 * @ProjectName: dhdemo
 * @Version: 1.0
 **/
@Configuration
public class CarServletConfig {
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        //注册自定义servlet
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new CarHttpServlet());
        registrationBean.addUrlMappings("/ipms/DH/Devices/1000019$0/Events", "/DH/Devices/1000019$0/Events");
       return registrationBean;
    }
}

 

package net.dlet.dhdemo.utils.dhutils;

import net.dlet.dhdemo.service.TestService;
import net.dlet.dhdemo.utils.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: 自定义servlet
 * 注意: urlPatterns 这里必须排列在一行,不能主动换行
 * @PackageName: net.dlet.dhdemo.utils.dhutils
 * @Name: HttpServletRequest
 * @Author: crue
 * @CreateDate: 2020/09/07 21:33
 * @DayNameFull: 星期一
 * @ProjectName: dhdemo
 * @Version: 1.0
 **/
//@WebServlet(name = "CarHttpServlet", urlPatterns = {"/ipms/DH/Devices/**","/DH/Devices/**"})
public class CarHttpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TestService testService = SpringUtil.getBean(TestService.class);
        testService.dealDhDataCustom(req, resp);
    }

}

 1、TestService中dealDhDataCustom函数实现如下:

 public String dealDhDataCustom(HttpServletRequest servletRequest, HttpServletResponse response) throws IOException {
        String encoding = "ISO-8859-1";

        //取Body数据 读出数据流
//        String[] inputStream = IOUtils.readLines(servletRequest.getInputStream());
        byte[] buffer = StreamUtils.getByteByStream(servletRequest.getInputStream(), servletRequest.getContentLength(), encoding);

        return dealBuffer(servletRequest, buffer, encoding);
}

2、StreamUtils类如下: 

package net.dlet.dhdemo.utils.dhutils;

import java.io.*;

/**
 * @Description: 处理InputStream流的工具类
 * @PackageName: net.dlet.dhdemo.utils.dhutils
 * @Name: StreamUtils
 * @Author: crue
 * @CreateDate: 2020/09/06 14:43
 * @DayNameFull: 星期日
 * @ProjectName: dhdemo
 * @Version: 1.0
 **/
public class StreamUtils {

    /**
     * 读取流中的数据将其转换为byte数组
     * @param is 输入流
     * @param contentLength 内容的长度
     * @param encodeing 编码方式
     * @return byte数组
     * @throws IOException 读或者写可能出现io异常
     */
    public static byte[] getByteByStream(InputStream is, int contentLength, String encodeing) throws IOException {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = is.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        is.close();
        return outSteam.toByteArray();

    }
}

值得注意的是,在用@WebServlet注解的时候,开始的时候为了美观,把urlPatterns后面的参数换行格式化了一下。然而模拟发送数据,死活无法进去,后面只好排成一排。

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值