0

I'm using Spring MVC Structure in my project. My project Structure is as follow:

project
    |
    |
    |Java Resources
    |       |
    |       src
    WebContent
    |       |
    |       |
            resources
                |
                |
                form
                    |
                    Notification.pdf

I want to get the Absolute path of Notification.pdf when I call the Controller. I also tried the following code:

File file = new File("Notification.pdf");
        String path = file.getAbsolutePath();
        System.out.println("path: "+path);

Output: C:\Users\JohnPC\Desktop\Notification.pdf

Output I want: D:\workspace\project\WebContent\resources\form\Notification.pdf

neilnikkunilesh
  • 363
  • 2
  • 13
  • 2
    `new File("Notification.pdf")` will create a handle to a file called Notification.pdf in the default path (which would be your user's home directory). This will _not_ search for a file with this name (there might even be several, which would it pick then?). Also note that the "resources" folder might not be part of the final application much like the "src" directory won't be. So what you need to know is: what is your application's root directory and what's the file's relative path inside your application (you might even only need to know that). – Thomas Feb 05 '20 at 12:25
  • What exactly do you need that absolute path of that file for? – Thomas Feb 05 '20 at 12:27
  • Does this answer your question https://stackoverflow.com/questions/17351043/how-to-get-absolute-path-to-file-in-resources-folder-of-your-project – Player_Neo Feb 05 '20 at 12:49

2 Answers2

0
getClass().getResources("Notification.pdf").getPath()
  • 1
    welcome to stack overflow. please consider elaborating and explaining how your code answers the OP's question – sao Feb 05 '20 at 13:22
0

The one thing that you are doing wrong is you are assuming when you create a file it is created in the current directory of the file that you are running.

But usually when you running a spring project, it is not the java file that you are running that runs, it is either the JAR that is made while you build the Spring project that runs, or the WAR file that is run in an application server. Hence your file is never created in the directory resources.. If you need to create files on server you should define a static directory on that server and then create files in that defined directory.

Because once you create the file in that static directory it will persist in the server. You can create the file as you have done in the question. Just add the path to the file in the code.

File file = new File("Path to the file" + "Notification.pdf"); String path = file.getAbsolutePath(); System.out.println("path: "+path);

Dhananjay Gupta
  • 316
  • 2
  • 11
  • I want to create the functionality, when user click on button it will download.Mainaly there is controller in that I have written 2 no of download file methods. When user click on button two PDF file will be download one by one like Notification.pdf, AdmissionForm.pdf. Notification.pdf is static file that is present in form folder and AdmissionForm.pdf will be generate first using JasperReports then it will download. – neilnikkunilesh Feb 05 '20 at 17:21