从InputStream到servletInputStream

我有这个InputStream:

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

如何将其转换为ServletInputStream?

我努力了:

ServletInputStream  servletInputStream = (ServletInputStream) inputStream;

但不起作用。

我的方法是这样的:

private static class LowerCaseRequest extends HttpServletRequestWrapper {

public LowerCaseRequest(final HttpServletRequest request) throws IOException, ServletException {

super(request);

}

@Override

public ServletInputStream getInputStream() throws IOException {

ServletInputStream servletInputStream;

StringBuilder jb = new StringBuilder();

String line;

String toLowerCase = "";

BufferedReader reader = new BufferedReader(new InputStreamReader(super.getInputStream()));

while ((line = reader.readLine()) != null) {

toLowerCase = jb.append(line).toString().toLowerCase();

}

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

servletInputStream = (ServletInputStream) inputStream;

return servletInputStream;

}

}

我正在尝试将所有请求都转换为小写。

回答:

试试这个代码。

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(myString.getBytes(StandardCharsets.UTF_8));

ServletInputStream servletInputStream=new ServletInputStream(){

public int read() throws IOException {

return byteArrayInputStream.read();

}

}

以上是 从InputStream到servletInputStream 的全部内容, 来源链接: utcz.com/qa/422387.html

回到顶部