我有一个表单,我想通过调用来提供AbstractResource

getRequestCycle().scheduleRequestHandlerAfterCurrent(target);


目标必须是IRequestHandler的实现。

我想传递以下AbstractResource对象。

public class ExcelResponseResource extends AbstractResource {

    @Override
    protected ResourceResponse newResourceResponse(Attributes attributes) {
        ResourceResponse resourceResponse = new ResourceResponse();
        resourceResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        resourceResponse.setTextEncoding("utf-8");
        resourceResponse.setFileName("SomeExport.xlsx");
        resourceResponse.setWriteCallback(new WriteCallback() {
            @Override
            public void writeData(Attributes attributes) {
                try {
                    SomeExport export = new SomeExport(arguments);
                    byte[] byteArray = ((ByteArrayOutputStream)export.getOutputStream()).toByteArray();
                    attributes.getResponse().write(byteArray);
                } catch (Exception e) {
                    log.error("Something went wrong during Excel generation!", e);
                }
            }
        });
        resourceResponse.disableCaching();

        return resourceResponse;
    }
}


我想知道如何将这只野兽放入ResourceStreamRequestHandler或类似的东西中。

提前致谢!

最佳答案

将AbstractResource更改为AbstractResourceStream,现在它就像一个吊饰一样工作。

ExcelResourceStream stream = new ExcelResourceStream();
ResourceStreamRequestHandler requestHandler = new ResourceStreamRequestHandler(stream);
getRequestCycle().scheduleRequestHandlerAfterCurrent(requestHandler);


ExcelResourceStream看起来像这样:

public class ExcelResourceStream extends AbstractResourceStream {

    private ByteArrayInputStream inputStream;

    @Override
    public String getContentType() {
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    }

    @Override
    public Bytes length() {
        return Bytes.bytes(inputStream.available());
    }

    @Override
    public InputStream getInputStream() throws ResourceStreamNotFoundException {
        if (inputStream == null) {
            SomeExport export = new SomeExport();
            try {
                byte[] byteArray = ((ByteArrayOutputStream)export.getOutputStream()).toByteArray();
                inputStream = new ByteArrayInputStream(byteArray);
            } catch (IOException ioe) {
                // STUB
            }
        }

        return inputStream;
    }

    @Override
    public void close() throws IOException {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

10-07 20:03