Wicket:在onSubmit之后下载文件

时间:2013-01-11 09:14:17

标签: java web wicket wicket-1.5

所以我拥有一个包含三列的DataView,其中一列是一个复选框列,允许用户检查他们想要下载哪些文件。

为了简单起见(我认为);我决定将文件压缩成一个zip文件,并在生成后提供服务。

这是我到目前为止所拥有的:

代码:


    Button downloadLogButton = new Button("downloadlogbutton") {
        private static final long serialVersionUID = 1L;
        @Override
        public void onSubmit() {
            // Some utility class I made that zips files
            LogUtility util = new LogUtility();
            util.zipLogFiles("sample", logs);
        }
    };

    Form logsForm = new Form("logsform") {

    };

    logsForm.add(downloadLogButton);

    CheckGroup<File> checkGroup = new CheckGroup<File>("logscheckgroup", new ArrayList<File>());
    WebMarkupContainer logsViewContainer = new WebMarkupContainer("datatable");
    DataView<File> logsView = new DataView<File>("logrows", new ListDataProvider<File>(logs)) {

        private static final long serialVersionUID = 1L;

        public void populateItem(final Item<File> item) {
            final File file = (File) item.getModelObject();
            item.add(new Check<File>("logdownloadcheck", item.getModel()));
            item.add(new Label("logname", file.getName()));
            SimpleDateFormat sdf = new SimpleDateFormat( "E, dd MMM yyyy  hh:mm a");
            item.add(new Label("logdatemodified", sdf.format(file .lastModified())));
        }
    };

    logsViewContainer.add(logsView);
    checkGroup.add(logsViewContainer);
    logsForm.add(checkGroup);
    add(logsForm);

如何生成下载后的zip文件?我有什么选择?我希望避免将其重定向到确认页面或Your download is ready页面。

更新

根据XaviLópez的回答,我在Button的{​​{1}}函数中添加了以下代码。

onSubmit

我得到org.apache.wicket.util.file.File log = new org.apache.wicket.util.file.File("/home/keeboi/Downloads/sample.zip"); IResourceStream resourceStream = new FileResourceStream(log); IRequestHandler target = new ResourceStreamRequestHandler(resourceStream); getRequestCycle().scheduleRequestHandlerAfterCurrent(target);

1 个答案:

答案 0 :(得分:5)

你可以像DownloadLink那样做,并从压缩文件中创建FileResourceStream。然后,只需更改当前请求周期的目标:

Button downloadLogButton = new Button("downloadlogbutton") {
    private static final long serialVersionUID = 1L;
    @Override
    public void onSubmit() {
        // Some utility class I made that zips files
        LogUtility util = new LogUtility();
        util.zipLogFiles("sample", logs);
        IResourceStream resourceStream = new FileResourceStream(
            new org.apache.wicket.util.file.File(someFile)); // Use whatever file you need here
        IRequestTarget t = new ResourceStreamRequestTarget(stream){
            @Override
            public String getFileName() {
                return "filename.zip";
            }
        }
        getRequestCycle().setRequestTarget(t);
    }
};

如果您想在下载后删除该文件,可以覆盖IRequestTarget#respond(RequestCycle),如下所示:

@Override
public void respond(RequestCycle requestCycle) {
    super.respond(requestCycle);
    // Delete the file
    ((FileResourceStream)getResourceStream()).getFile().delete();
}

以下相关问题也很有用: How to use Wicket's DownloadLink with a file generated on the fly?