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页面。

更新

基于 Xavi López 的回答,我在我ButtononSubmit函数中添加了以下代码。

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);

我得到了HTTP Error 404: Not Found

4

1 回答 1

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 on the fly? .

于 2013-01-11T11:03:50.093 回答