user1028383
user1028383

Reputation: 1

Show list of files on server

We are saving .csv files on a tomcat6.0 server that are sent via cron to an external vendor. On occasion, the send doesn't work and we need to get the .csv file from the server and email it to the vendor. Instead of having to log in to the server, I am trying to add another function on our webpage (that lives on the same server) that will allow administrators to download the file to their desktop and email it that way. If I know the name of the file, all is well and I have that part working, but I need to be able to select a file from the directory on the server. Finally my question: How can I show the list of files from a particular directory in my java servlet?

Upvotes: 0

Views: 1745

Answers (2)

Shamaila Tahir
Shamaila Tahir

Reputation: 998

Following function will return an arraylist of files present inside a folder.

public ArrayList<String> getReportNames() throws IllegalArgumentException {
    String path=getServletContext().getRealPath("/WEB-INF");        
    File[] list = new File(path+"/YOUR_FOLDERNAME_INSIDE_WEBINF").listFiles(new MyFileNameFilter());
    ArrayList<String> fileNames=new ArrayList<String>();
    for (File file: list)
        fileNames.add(file.getName());
    return fileNames;
}

Upvotes: 1

John B
John B

Reputation: 32949

List the files in the directory (using File.listFiles) and display a page with the list. Unclear if you are asking for something more.

Upvotes: 0

Related Questions