【问题标题】:WebResponse.getOutputStream() in Wicket 1.5?Wicket 1.5 中的 WebResponse.getOutputStream()?
【发布时间】:2011-11-17 06:41:54
【问题描述】:

此代码在 1.4 中为我工作:

WebResponse response = (org.apache.wicket.request.http.WebResponse) getResponse();
response.setAttachmentHeader("List.xls");
response.setContentType("application/ms-excel");
OutputStream out = response.getOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(out);
.....
.....
workbook.write();
workbook.close();

我在 1.5 中看到没有 WebResponse.getOutputStream() - 但它没有被标记为已弃用?

我查看了 1.5 迁移指南,但看不到任何明显的解决方案。

谁能告诉我我应该如何在 1.5 中做到这一点。

【问题讨论】:

    标签: wicket wicket-1.5


    【解决方案1】:

    您可以将Response 包装在OutputStream 中:

    public final class ResponseOutputStream extends OutputStream {
        private final Response response;
        private final byte[] singleByteBuffer = new byte[1];
        public ResponseOutputStream(Response response) {
            this.response = response;
        }
        @Override
        public void write(int b) throws IOException {
            singleByteBuffer[0] = (byte) b;
            write(singleByteBuffer);
        }
        @Override
        public void write(byte[] b) throws IOException {
            response.write(b);
        }
        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            if (off == 0 && len == b.length) {
                this.write(b);
            } else {
                super.write(b, off, len);
            }
        }
    }
    

    【讨论】:

    • 谢谢,我会试试这个。我还不能测试。
    【解决方案2】:

    这个问题昨天已经解决了。将成为 Wicket 1.5.4 的一部分。 但是对于这个用例,您应该使用资源。查看 ResourceLink 的实现。

    【讨论】:

    • 我正在使用 jExcelAPI 库。我需要将 OutputStream 作为参数提供给 Workbook.createWorkbook 我没有将输出写入磁盘 - 想要将其直接流式传输到浏览器。你的建议能解决这个问题吗?
    • 是的,它会 - 您使用 ResourceLink 来保存资源,例如一个 AbstractResource。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多