开发者

How to access local files on server in JBoss application?

I am looking to access all files in a local directory in a JBoss application. I can put the directory anywhere in my war including WEB-INF if necessary. I then want to access each file in the directory sequentially. In a normal application if the directory was in the run location I could do something like:

File f = new File("myDir");
if(f.isDirectory && f.list().length != 0)
{
    for(String fileName : f.list())
    {
        //do Read-Only stuff with fileName
    }
}

I'm looking for a best-practices s开发者_如何学Goolution, so if I'm going about this wrong then please point me to the right way to access an unknown set of resources.


First thing to note: you're only going to get this to work if you have an exploded WAR, or possibly if the servlet container explodes the WAR for you.

With that caveat in mind, you could use ServletContext.getRealPath() as your starting point. You'd need to know the name of at least one file in the webapp's root directory, and go from there:

String knownFilePath = servletContext.getRealPath("knownFile");
File webAppRootDir = new File(knownFilePath).getParentFile();

// and then as per the question
File f = webAppRootDir ;
if(f.isDirectory && f.list().length != 0)
{
    for(String fileName : f.list())
    {
        //do Read-Only stuff with fileName
    }
}

Getting hold of ServletContext is left as an exercise for the reader.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜