Wicket serving images from File System

佐手、 提交于 2019-12-03 16:28:53

You could use it as a shared resource:

    public class WicketApplication extends WebApplication {
        @Override
        public Class<HomePage> getHomePage() {
            return HomePage.class;
        }
        @Override
        public void init() {
            super.init();
            getSharedResources().add("downloads", new FolderContentResource(new File("C:\\Users\\ronald.tetsuo\\Downloads")));
            mountResource("downloads", new SharedResourceReference("downloads"));
        }
        static class FolderContentResource implements IResource {
            private final File rootFolder;
            public FolderContentResource(File rootFolder) {
                this.rootFolder = rootFolder;
            }
            public void respond(Attributes attributes) {
                PageParameters parameters = attributes.getParameters();
                String fileName = parameters.get(0).toString();
                File file = new File(rootFolder, fileName);
                FileResourceStream fileResourceStream = new FileResourceStream(file);
                ResourceStreamResource resource = new ResourceStreamResource(fileResourceStream);
                resource.respond(attributes);
            }
        }
    }

You can still use ResourceReferences with global IDs. You just have to use a SharedResourceReference. This is probably better, too.

add(new Image("image", new SharedResourceReference("mySharedResourceRef", parameters));

I would try to avoid building paths from URL parameters. This can easily end up in security leaks.

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