1

The project I inherited has an excel template that gets populated when the user performs an action. Whoever programmed it originally did this in the ApplicationProperties.properties file.

#LOCAL
TemplatePath=C:/.../web/fileName.xls
#LIVE
#TemplatePath=/usr/share/tomcat/webapps/projectName/fileName.xls

There are a few different files that are set up like this so every time we have to deploy I have to make sure to go into the application properties file and update them. I would like to refactor the code so that can be done with only a relative path to the file. If this were a .net project I would use Server.MapPath() to get the current path and then append the directory/file name onto it.

Is there a way to do that in Java? Or is there a better way? I noticed something about putting the file in WEBINF/classes when I was googling the issue. Is that better? How does that work? I'm new to Java.

William
  • 1,457
  • 5
  • 21
  • 46

2 Answers2

3

If you put it in your WEB-INF/classes directory, then the files will be available in your classpath and you can refer to them by using MyClass.class.getResourceAsStream("classpath path"). This is better than what you're trying to do, because the classpath is under your control. When you use a relative path to a file, your code can break if different IDEs use different working directories.

Of course, you can't easily write to those files if you do that since they'll be in your war file. But I'm not sure if that's a requirement for you or not.

Here's a great answer that seems relevant to your question: getResourceAsStream() vs FileInputStream

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
1

You can put your XLS template in a java package, something like that:

MyWebApp
+---src
|   \---org
|       \---paulvargas
|           \---test
|               |   TestServlet.java
|               |
|               \---resources
|                       template.xls
|
\---WebContent
    \---WEB-INF
        |   web.xml
        |
        \---lib

When the application is deployed, the file template.xls is automatically placed in the classes directory in the directory indicated by the java package, WEB-INF/classes/org/paulvargas/test/resources/. So, to read the file:

package org.paulvargas.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        ClassLoader classLoader = Thread.currentThread()
            .getContextClassLoader();
        InputStream inputStream = classLoader
            .getResourceAsStream("org/paulvargas/test/resources/template.xls");

        // Populate the template

    }

}

See also:

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148