Link to home
Start Free TrialLog in
Avatar of DESEI-SELB
DESEI-SELB

asked on

where do we put the text files?

I have an application which has links to some text files. I  am not sure where do put these text files. I am thinking if they will be included in the ear file, then the applicaion has to keep getting redoployed every time there is small change in the one of the text files?

thanks,
Avatar of Hegemon
Hegemon
Flag of United Kingdom of Great Britain and Northern Ireland image

Correct. You may want to deploy them separately and redeploy only text files in case of changes.
Avatar of tanhung
tanhung

Don't put them in the ear file. Put them in a separate directory; or even put them in database.
Avatar of Gurvinder Pal Singh
Put them in an external folder, and keep their location path in the properties file for your application.

So that when you change the files or add more files you simply need to go to that location to read those files.
Outside webapp.
One way (there are other ways) is to do the following:
Assume a virtual directory /data/ with your text files:

In the WEB-INF/web.xml:

     <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
   <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/data/*</url-pattern>
        <url-pattern>*.html</url-pattern>
        <url-pattern>*.json</url-pattern>
       ...

And let the servlet stream it out:
Something like:

import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.FileUtils;

    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String realPath = null;
        String path = request.getRequestURI();
        if (path.startsWith(fromPath)) {
            realPath = toPath + path.substring(fromPath.length());
        } else {
            Logger.getLogger(StaticContentElsewhereController.class.getName()).log(Level.SEVERE, "Unknown path: {0}", path);
            realPath = request.getPathTranslated();
        }
        for (Enumeration<String> en = request.getHeaderNames(); en.hasMoreElements(); ) {
            String name = en.nextElement();
            String value = request.getHeader(name);
            LOGGER.log(Level.INFO, "Header {0}: {1}", new Object[]{name, value});
        }
        if (realPath != null) {
            File sourceFile = new File(realPath);
            InputStream in = FileUtils.openInputStream(sourceFile);
            OutputStream out = response.getOutputStream();
            Streams.copy(in, out, false);
        }
        return null;
    }



Avatar of DESEI-SELB

ASKER

This is too complicated.
gurvinder372, you suggested to put the path in the property file. can you please explain more. where is the property file? sorry I am very new to this. thanks,
ASKER CERTIFIED SOLUTION
Avatar of joop_eggen
joop_eggen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial