我有这个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;
}
}
我正在尝试将我的所有请求转换为小写。
答案 0 :(得分:21)
我的建议:不要创建ByteArrayInputStream
,只使用已经从getBytes
方法获得的字节数组。这应该足以创建ServletInputStream
。
不幸的是,aksappy的回答只会覆盖read
方法。虽然这在Servlet API 3.0及更低版本中已经足够了,但在更高版本的Servlet API中,您需要实现三个更多方法。
这是我的类的实现,虽然它变得很长(由于Servlet API 3.1中引入了新方法),但您可能需要考虑将其分解为嵌套甚至顶级类。 / p>
final byte[] myBytes = myString.getBytes("UTF-8");
ServletInputStream servletInputStream = new ServletInputStream() {
private int lastIndexRetrieved = -1;
private ReadListener readListener = null;
@Override
public boolean isFinished() {
return (lastIndexRetrieved == myBytes.length-1);
}
@Override
public boolean isReady() {
// This implementation will never block
// We also never need to call the readListener from this method, as this method will never return false
return isFinished();
}
@Override
public void setReadListener(ReadListener readListener) {
this.readListener = readListener;
if (!isFinished()) {
try {
readListener.onDataAvailable();
} catch (IOException e) {
readListener.onError(e);
}
} else {
try {
readListener.onAllDataRead();
} catch (IOException e) {
readListener.onError(e);
}
}
}
@Override
public int read() throws IOException {
int i;
if (!isFinished()) {
i = myBytes[lastIndexRetrieved+1];
lastIndexRetrieved++;
if (isFinished() && (readListener != null)) {
try {
readListener.onAllDataRead();
} catch (IOException ex) {
readListener.onError(ex);
throw ex;
}
}
return i;
} else {
return -1;
}
}
};
根据您的要求,您可能还想覆盖其他方法。正如romfret指出的那样,建议覆盖一些方法,例如close
和available
。如果您不实现它们,流将始终报告有0个字节可供读取,close
方法将不会影响流的状态。您可以在不覆盖skip
的情况下离开,因为默认实现只会多次调用read
。
@Override
public int available() throws IOException {
return (myBytes.length-lastIndexRetrieved-1);
}
@Override
public void close() throws IOException {
lastIndexRetrieved = myBytes.length-1;
}
不幸的是,由于匿名类的性质,你很难编写一个有效的close
方法,因为只要一个流实例没有被Java垃圾收集,它就会保持对字节数组的引用,即使该流已被关闭。
但是,如果将类分解为嵌套或顶级类(或者甚至是具有从定义它的行调用的构造函数的匿名类),myBytes
可以是非最终字段而不是最终的局部变量,您可以添加如下行:
myBytes = null;
到你的close
方法,这将允许Java释放字节数组占用的内存。
当然,这需要你编写一个构造函数,例如:
private byte[] myBytes;
public StringServletInputStream(String str) {
try {
myBytes = str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("JVM did not support UTF-8", e);
}
}
如果您想支持标记/重置,您可能还想覆盖mark
,markSupported
和reset
。我不确定它们是否真的被你的容器调用过。
private int readLimit = -1;
private int markedPosition = -1;
@Override
public boolean markSupported() {
return true;
}
@Override
public synchronized void mark(int readLimit) {
this.readLimit = readLimit;
this.markedPosition = lastIndexRetrieved;
}
@Override
public synchronized void reset() throws IOException {
if (markedPosition == -1) {
throw new IOException("No mark found");
} else {
lastIndexRetrieved = markedPosition;
readLimit = -1;
}
}
// Replacement of earlier read method to cope with readLimit
@Override
public int read() throws IOException {
int i;
if (!isFinished()) {
i = myBytes[lastIndexRetrieved+1];
lastIndexRetrieved++;
if (isFinished() && (readListener != null)) {
try {
readListener.onAllDataRead();
} catch (IOException ex) {
readListener.onError(ex);
throw ex;
}
readLimit = -1;
}
if (readLimit != -1) {
if ((lastIndexRetrieved - markedPosition) > readLimit) {
// This part is actually not necessary in our implementation
// as we are not storing any data. However we need to respect
// the contract.
markedPosition = -1;
readLimit = -1;
}
}
return i;
} else {
return -1;
}
}
答案 1 :(得分:9)
试试这段代码。
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(myString.getBytes(StandardCharsets.UTF_8));
ServletInputStream servletInputStream=new ServletInputStream(){
public int read() throws IOException {
return byteArrayInputStream.read();
}
}
答案 2 :(得分:0)
你只能投这样的东西:
ServletInputStream servletInputStream = (ServletInputStream) inputStream;
如果您尝试转换的inputStream实际上已经是ServletInputStream。如果它是InputStream的其他一些实现,它会抱怨。你不能将一个物体投射到它不是的东西上。
在Servlet容器中,您可以从ServletRequest获取ServletInputStream:
ServletInputStream servletInputStream = request.getInputStream();
那么,你究竟想做什么?
修改
我很感兴趣为什么要将您的请求转换为小写 - 为什么不让您的servlet不区分大小写呢?换句话说,您的代码以小写的方式可以将请求数据复制到您的servlet中,然后它可以在那里处理它...总是寻找最简单的解决方案!