本文介绍了如何像 Servlet 一样在 Wicket 中处理二进制流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Servlet,我可以执行以下操作来处理二进制流:
Using Servlet, I can do the following to process binary stream:
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream inputStream = req.getInputStream();
byte[] data = IOUtils.toByteArray(inputStream);
// ...
Result result = process(data);
// ...
ServletOutputStream op = resp.getOutputStream();
result.writeTo(resp.getOutputStream());
}
我如何在 Wicket 中做到这一点?(创建页面后我没有任何线索扩展 WebPage 类)
How I can do this in Wicket? (I have no clue after creating a page extends WebPage class)
推荐答案
我现在使用AbstractResourceStreamWriter来实现解决方案:
I am now using AbstractResourceStreamWriter to implement the solution:
public MyApiPage(final PageParameters pageParameters) {
HttpServletRequest httpServletRequest = getHttpRequest();
byte[] data = IOUtils.toByteArray(httpServletRequest.getInputStream());
IResourceStream resourceStream = new MyApiResourceStream(data);
getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(resourceStream));
}
private class MyApiResourceStream extends AbstractResourceStreamWriter {
private byte[] data;
public MyApiResourceStream(byte[] data) {
this.data = data;
}
@Override
public void write(Response webResponse) {
byte[] convertedData = // do some works here...
webResponse.write(convertedData);
}
}
这篇关于如何像 Servlet 一样在 Wicket 中处理二进制流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!