InputStream to servletInputStream

前端 未结 3 873
后悔当初
后悔当初 2020-12-19 00:50

I have this InputStream:

InputStream inputStream = new ByteArrayInputStream(myString.getBytes(StandardCharsets.UTF_8));

How can I convert t

3条回答
  •  旧时难觅i
    2020-12-19 01:19

    You can only cast something like this:

    ServletInputStream  servletInputStream = (ServletInputStream) inputStream;
    

    if the inputStream you are trying to cast is actually a ServletInputStream already. It will complain if it's some other implementation of InputStream. You can't cast an object to something it isn't.

    In a Servlet container, you can get a ServletInputStream from a ServletRequest:

    ServletInputStream  servletInputStream = request.getInputStream();
    

    So, what are you actually trying to do?

    EDIT

    I'm intrigued as to why you want to convert your request to lower-case - why not just make your servlet case-insensitive? In other words, your code to lower-case the request data can be copied into your servlet, then it can process it there... always look for the simplest solution!

提交回复
热议问题