Wicket从文件系统提供图像 [英] Wicket serving images from File System

查看:150
本文介绍了Wicket从文件系统提供图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我对Wicket很新,我在使用资源引用方面遇到了一些困难。我正在使用wicket 1.5.4并有以下问题:我将图像存储在文件系统上。我有类ImageElement,它保存相对于配置的rootFilePath的文件路径的一部分(即dir1 / dir2 / img1.png)。在页面上我按如下方式添加Image:

I am pretty new to Wicket and i have some difficulties with using resource references. I am using wicket 1.5.4 and have following problem: I store images on the file system. I have class ImageElement which holds part of the file path relative to configured rootFilePath (i.e dir1/dir2/img1.png). On the page I add Image as follows:

new Image("id",ImagesResourceReference.get(), pageParameters)

其中页面参数包括图像路径参数(path =/ dir1 / dir2 / img1.png)。我的问题是:

where page parameters includes image path parameter (path="/dir1/dir2/img1.png"). My questions are:


  • 这是从文件系统提供图像的最简单方法吗?

  • 使用ResourceReference和静态方法是否可以?或者我应该每次构建新的ResourceReference?我看到在以前的版本中可以使用新的ResourceReference(globalId),但似乎不再是这种情况了。如果是这样,全球资源参考是什么?据我所知,资源引用应该是资源的工厂,因此为每个资源请求创建新工厂会很奇怪。

  • 最后一个问题是,如何以更好的方式将路径传递给图像,以便在ImageResource上调用response方法后,我不必连接索引参数来构建路径。

  • 让它以高效简单的方式工作的最佳方案是什么,我在Wicket in action中看到了这个例子,但这是为了从db和动态图像生成我不确定它是否符合我的情况

  • Is it the simplest way of serving images from the file system?
  • Is it ok to use ResourceReference with static method? or I should construct each time new ResourceReference? I saw that in previous version it was possible to use new ResourceReference(globalId), but it seems not to be the case anymore. If so what is the global resource reference for? So far as I understand resource reference is supposed to be factory for resources so it would be rather strange to create new factory for each resource request.
  • The last question is, how can i pass the path to the image in a better way so that i do not have to concatenate indexed parameters to build the path once respond method is invoked on ImageResource.
  • What would be the best scenario to get it working in efficient and simple way, i saw the example in 'Wicket in action', but this is meant for dynamic image generation from db and am not sure if it suites for my case

我在/ images路径下安装在Application中的ResourceReference的实现看起来像如下:

My implementation of ResourceReference which I mounted in Application under "/images" path, looks as follows:

public class ImagesResourceReference extends ResourceReference {

private static String rootFileDirectory;

private static ImagesResourceReference instance;

private ImagesResourceReference() {
    super(ImagesResourceReference.class, "imagesResourcesReference");
}

public static ImagesResourceReference get() {
    if(instance == null) {
        if(StringUtils.isNotBlank(rootFileDirectory)) {
            instance = new ImagesResourceReference();
        } else {
            throw new IllegalStateException("Parameter configuring root directory " +
                    "where images are saved is not set");
        }
    }
    return instance;
}

public static void setRootFileDirectory(String rootFileDirectory) {
    ImagesResourceReference.rootFileDirectory = rootFileDirectory;
}

private static final long serialVersionUID = 1L;

@Override
public IResource getResource() {

    return new ImageResource(rootFileDirectory);
}

private static class ImageResource implements IResource {

    private static final long serialVersionUID = 1L;

    private final String rootFileDirectory;

    public ImageResource(String rootFileDirectory) {
        this.rootFileDirectory = rootFileDirectory;
    }

    @Override
    public void respond(Attributes attributes) {

         PageParameters parameters = attributes.getParameters();
         List<String> indexedParams = getAllIndexedParameters(parameters);
         if(!indexedParams.isEmpty() && isValidImagePath(indexedParams)) {
             String pathToRequestedImage = getImagePath(indexedParams);
             FileResourceStream fileResourceStream = new FileResourceStream(new File(pathToRequestedImage));
             ResourceStreamResource resource = new ResourceStreamResource(fileResourceStream);
             resource.respond(attributes);
         }
    }

    private boolean isValidImagePath(List<String> indexedParams) {
        String fileName = indexedParams.get(indexedParams.size() -1);
        return !FilenameUtils.getExtension(fileName).isEmpty();
    }

    private List<String> getAllIndexedParameters(PageParameters parameters) {
        int indexedparamCount = parameters.getIndexedCount();
        List<String> indexedParameters = new ArrayList<String>();
        for(int i=0; i<indexedparamCount ;i++) {
            indexedParameters.add(parameters.get(i).toString());
        }
        return indexedParameters;
    }

    private String getImagePath(List<String> indexedParams) {
        return rootFileDirectory + File.separator + StringUtils.join(indexedParams, File.separator);
    }

}

任何帮助和建议表示赞赏!在此先感谢。

Any help and advices appreciated! Thanks in advance.

推荐答案

您可以将它用作共享资源:

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

这篇关于Wicket从文件系统提供图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆