0

We have a .war file deployed in our app server. We need to access a logo image inside that .war file, like


  test.war
        |_ skin/
              |_ logo.gif

And in our application we keep a property file which poins to this image location, in order to load it:

imagepath = D:/server/.../test.war/skin/logo.gif

It was working fine.

Problem

Now we changed our test.war to compressed (zipped) format, and our current implementation couldn't load it.Our intention is to load the image without changing current implementation.That means can we solve it by just changing the image path in properties file?(I know we can acces zipped file in java using zip utils, unfortunately we could not change code) We tried

imagepath = jar:file:/D:/server/.../test.war!/skin/logo.gif

and didnt work . Any hope?

Tom Sebastian
  • 3,373
  • 5
  • 29
  • 54
  • Without opening the war it is not possible. But normally, once deployed your war has been exploded in the `webapps` folder (or something similar). And then you should find your image. – YMomb Dec 12 '13 at 12:13
  • we are using Jboss as 7. I didn't find any exploded folder structure for the war file. I noticed that thing in tomcat, it will work u said – Tom Sebastian Dec 12 '13 at 12:21
  • Maybe you can exploded your war first as explained [here][1]. [1]: http://stackoverflow.com/questions/487363/how-can-i-get-jboss-to-explode-a-deployed-war-file – YMomb Dec 12 '13 at 12:22
  • ok, any other option without extracting war file? – Tom Sebastian Dec 12 '13 at 12:29
  • 1
    possible duplicate of [How to load resource from jar file packaged in a war file?](http://stackoverflow.com/questions/4585553/how-to-load-resource-from-jar-file-packaged-in-a-war-file) – Raedwald Dec 12 '13 at 13:18

1 Answers1

0

Here is code (Servlet getMethod) which downloads web.xml and zips it. I quess it will be easy for you to convert it for your purpose: just remove zip stream.

The main trick is getRealPath.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            final String docxzip = "zip";

            // vzemi datoteko iz web xml
            String webXMLFile = getServletContext().getRealPath("/WEB-INF/web.xml");

            response.setContentType("application/zip");
            response.setHeader("Content-Disposition","inline; filename=output.zip;");

            ServletOutputStream outputStream = response.getOutputStream();
            ZipOutputStream zip = new ZipOutputStream(outputStream);
            zip.putNextEntry(new ZipEntry("web.xml"));

            byte[] b = new byte[1024];
            int len;
            FileInputStream fis = new FileInputStream(webXMLFile);
            while ((len = fis.read(b)) != -1) {
                zip.write(b, 0, len);
            }
            //response.setHeader("Content-Length", (new Integer(len)).toString());
            fis.close();
            zip.flush();
            zip.close();
            outputStream.flush();
            final PrintWriter out = response.getWriter();
            out.print(docxzip);
            out.flush();
            return;
        } catch (Exception ex) {
            HttpUtils.noData(response, ex.toString());
            return;
        }
    }

P.S. Access to files inside WAR may be denied on certain java servers.

Mitja Gustin
  • 1,723
  • 13
  • 17