2 Replies Latest reply on Aug 20, 2009 5:41 AM by sepp deinhofer

    reading a file stored outside the .war

    sepp deinhofer Newbie

      hi,

      we are developing a servlet here that will read the content of a couple of files and outputs binary data.
      rather than storing our base-files in a database (which would be quite slow, given the size of our files) or putting them in the .war (which would prevent us from later adding files short of rebuilding/redeploying) we want to store them in a local folder of the system where our jboss-instance runs and open them as files via FileInputStream:

      o) where would be the "proper" directory to store those files?
      o) is there a "proper way" to do this (obvious extensions will be a file-upload/management servlet as a next step)?

      thanks for any help that can point us into the right direction!
      cheers,
      s


        • 1. Re: reading a file stored outside the .war
          Francesco Marchioni Apprentice

          You have a few solution which are not JBoss specific, like looking for the file in the filesystem

          try
           {
           url = new File(fileName).toURL();
           is = url.openStream();
           return is;
           }
           catch (MalformedURLException ignore)
           {
           }
           catch (IOException ignore)
           {
          
           }


          or in the current classpath:

          url =
          Thread.currentThread().getContextClassLoader().getResource(fileName);
           if (url != null)
           {
           try
           {
           return url.openStream();
           }
           catch (IOException ioe)
           {
          
           }
           }


          With JBoss you have one service named "Properties MBean Service" which allows loading a property file from an available URL.
          See this tip :
          http://www.mastertheboss.com/en/jboss-howto/42-jboss-config/151-how-to-inject-system-properties-to-jboss-.html

          Hope it helps
          Francesco

          • 2. Re: reading a file stored outside the .war
            sepp deinhofer Newbie

            thanks!
            what we did was to use the "properties mbean service" from the link together with an input stream opened via an url. this seemed like the most future-proof way of setting this up (web-service and asset-storage are completely decoupled from each other):

            property-service.xml:

            <server>
             <mbean code="org.jboss.varia.property.SystemPropertiesService"
             name="jboss:type=Service,name=SystemProperties">
            
             <attribute name="Properties">
             AssetPath=file:///g|projects/ourProject/assets/test.txt
             </attribute>
            
             </mbean>
            </server>


            and in the servlet:

            String path = System.getProperty("AssetPath");
            URL url = new URL(path);
            InputStream is = url.openStream();