tapestry 5.4列出webapp文件夹中的所有图像

时间:2017-03-06 08:19:17

标签: java tapestry

我的问题是,我想列出所有的图像 项目/ SRC /主/ web应用/图像

我知道如果我知道图像的名称,我可以像这样建立一个URL:

assetSource.getContextAsset(IMAGESLOCATION + imageName, currentLocale).toClientURL();

但是,如果我不知道所有的图像名称呢?

提前感谢您的答案!

2 个答案:

答案 0 :(得分:3)

Web应用程序(以及tapestry)也不知道/关心文件的绝对路径,因为它可以部署在任何地方。 您可以通过调用HttpServletRequest的getRealPath来获取某个文件的绝对路径。

    @Inject
    private HttpServletRequest request;
    ...
    // get root folder of webapp
    String root = request.getRealPath("/");
    // get abs path from any relative path
    String abs = root + '/' + relPath;
不推荐使用HttpServletRequest的getRealPath,建议使用ServletContext.getRealPath,但获取ServletContext并不容易。

我更喜欢使用WebApplicationInitializer实现

    public class AbstractWebApplicationInitializer implements WebApplicationInitializer {


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Here we store ServletContext in some global static variable
        Global.servletContext = servletContext;
        ....
    }

答案 1 :(得分:0)

您基本上需要能够读取给定文件夹中的文件。这是一些非常基本的代码,它将遍历文件夹中的所有文件:

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
  }
  // else, it's a directory
}

所有导入都应来自java.io package

相关问题