Download a zip file through wicket

南楼画角 提交于 2019-12-23 13:15:08

问题


I am using wicket framework, and I have made a zip file by Java code, I want to have a link to download it, I don't know if it is possible or I should make the zip file by wicket (but not Java) and then have a link to download.


回答1:


Take a look at ZipResourceStream. With this class you can generate zip contents of a directory on the fly, and use a org.apache.wicket.markup.html.link.ResourceLink with ResourceStreamResource to link to it.

File file = new File(path);
IResourceStream resStream = new ZipResourceStream(file);
ResourceStreamResource resource = new ResourceStreamResource(resStream);
ResourceLink link = new ResourceLink("link", resource);
add(link);

Alternatively, if you prefer to zip the file with another tool, you can use DownloadLink:

File zipFile = generateZipFile();
IModel fileModel = new Model(zipFile);
add(new DownloadLink("dllink", fileModel);

If you prefer to generate the File on the fly in the Link's onClick, take a look at this question: How to use Wicket's DownloadLink with a file generated on the fly?



来源:https://stackoverflow.com/questions/7851913/download-a-zip-file-through-wicket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!