0

I´m developing a web project in eclipse with a servlet in the backend (using tomcat 8). In the servlet i want to reference two large files which contain data that should be read. Until now i was just using hardcoded filepaths as strings to reference the files , e.g. like this: "C:\\data\\file1"

I want to store the files within the project, so that the relative path to them stays the same no matter on which computer i´m running the application.

This subject came up sometimes here and i´ve tried out things like

System.getProperty("catalina.base");

or

System.getProperty("user.dir");

but both methods point to completely different locations then where my files are stored.

So if the file is stored in the project like this:

"server\\webapps\\project\\data\\file1"

and the servlet is contained in the package folder with the other .java files, how can i get the path to file1 as a string?

flixe
  • 626
  • 1
  • 11
  • 36
  • Do you want to be able to modify the file without modifying the application and redeploying it, or is this file read-only, and thus embeddable in the application itself? Do you realize that, once deployed in a test or production environment, there won't be any eclipseworkspace folder on the machine? – JB Nizet Jan 05 '16 at 16:55
  • The file should be read-only. I´m sorry, i corrected the mistake. I didn´t mean eclipse workspace but basically the directory containing all the webapps deployed on the server. I just struggle understanding how it basically works to point to such a static file within the project no matter on which server i have deployed the project. – flixe Jan 05 '16 at 17:04

2 Answers2

1

If the file is part of the webapp, you must not consider it as a file on the file system. A Java EE webapp is deployed as a war file, which is basically a zip file. Some servers extract this war file to the disk, some don't, but you don't need to care, because the Java API provides everything you need to read resources embedded in the application.

If it is, in the deployed archive, inside WEB-INF/classes, then it is part of the classpath of the application must be loaded using the class loader:

InputStream in = MyServlet.class.getResourceAsStream("/com/foo/bar/file.txt");

where com.foo.bar is the package where the file is located. For a file to end up at that location in the archive, you would put it, with the Java source files, in the package com.foo.bar. Eclipse will "compile" this file by simply copying it.

If it is elsewhere in the archive, for example in /WEB-INF/file.txt, then you should use

InputStream in = servletContext.getResourceAsStream("/WEB-INF/file.txt");

To end up in /WEB-INF/ inside the archive, the file should be located, in a typical eclipse project, in WebContent/WEB-INF.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you, i stored the files in the package and in the war file they are in WEB-INF/classes together with the source files. My problem now is, that i want to open these files with a method based on the RandomAccessFile class, that takes the name of a file in form of a string as argument. The Inputstream object is not valid for that method, so is there an easy way to get the filename from that Inputstream object? – flixe Jan 05 '16 at 20:15
  • There is no file. Just a resource in a zip file. Why do you need a RandomAccessFile? – JB Nizet Jan 05 '16 at 22:36
  • The file/resource i want to read from is a special format called "netcdf" or ".nc". To handle this kind of format i´m using a method from a external library class which extends RandomAccessFile. This worked absolutely fine when i used the absolute filepath as a string as an argument. And since i´m not the author of this external library i cannot and don´t want to change this way of opening the resource. – flixe Jan 06 '16 at 10:38
  • Then you can simply, at startup, read everything from the resource and copy it to a temporary file, then use that class on your temporary file. – JB Nizet Jan 06 '16 at 10:47
0

I found a simple solution in this topic: how to load/reference a file as a File instance from the classpath

Except i´m not converting the URL object into a URI but into a string. Haven´t tested it on another server, but at least it works as it should for now.

Community
  • 1
  • 1
flixe
  • 626
  • 1
  • 11
  • 36