简体   繁体   中英

How to Dynamically get the path of a folder

i am trying to list Files Under a folder which is in my project path, I am working on java Servlets

I am doing It like this File folder = new File("D:/Vivek/Touchpoint/MirrorImage/WebContent/Image/"); , I want to get this Path dynamic because In future I am going to Deploy it on some other system So have to take That path

I have tried InputStream input = getServletContext().getResourceAsStream("D:/Vivek/Touchpoint/MirrorImage/WebContent/Image"); , but as of InputStream I am not able to loop after

I am doing whole code like this

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
             String DirectoryName="";
                File folder = new File("D:/Vivek/Touchpoint/MirrorImage/WebContent/Image/"); // Setting the path manually 
                File[] listOfFiles = folder.listFiles(); //creating an array to loop through
            //  System.out.println(folder);

                for (int i = 0; i < listOfFiles.length; i++) {
                  if (listOfFiles[i].isFile()) {
                 //   System.out.println("File " + listOfFiles[i].getName());
                  } else if (listOfFiles[i].isDirectory()) {
                     test=listOfFiles[i].getName();
                     System.out.println("Directory " + DirectoryName);
                  }

                }


}

这个
Is my project structure

I have tried System.out.println(new File(".").getAbsolutePath()); but it prints C:\\Windows\\system32\\.

You can try something like:

String path = request.getServletContext().getRealPath("/Image");
File folder = new File(path);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM