Results 1 to 4 of 4

Thread: Best way to specify file paths in contexts

  1. #1
    Join Date
    Aug 2004
    Location
    Toronto
    Posts
    8

    Default Best way to specify file paths in contexts

    Hi there,

    I have a particular requirement that I can't seem to find a "spring" way to solve. The requirement is simple, and at the root of it, it involves writing a file to a web application directory which then needs to be read and included as an image like a regular image src in a JSP. At issue is how to configure this file path using spring without relying on Spring specific APIs and without providing a hardcoded path.

    I have a service that procudes an image. This image needs to be written into a path. I would like to configure this service with a property which contains the path. So for example:

    Code:
    class ImageServiceImpl implements ImageProvider {
        private String saveFilePath
        public void setSaveFilePath(String pathToSet) {
             this.saveFilePath = pathToSet;
        }
    }
    And in my applicationContext.xml I want to specify:
    Code:
     ....
      
      <bean name="serviceProvider" class="ImageServiceImpl">
          <property name="saveFilePath"><value>/webroot/somedir</value></property>
      </bean>
     ....
    Is there a simple way to do this that will assume the path is relative to the web root?

    Many thanks,
    jc

  2. #2
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    You may want to try the following :
    create a factoryBean called for instance AbsoluteFilePathFactoryBean, which has a property of type Resource and which in its getObject method returns the resource.getFile().getAbsolutePath().
    This way, you can configure your string property for your spring agnostic POJO using an inner bean of this type in which you're passing the relative name of your location, which is converted into a spring resource relative to the root of your webapp and finally flatten into the file system absolute name.

    HTH
    Olivier

  3. #3
    Join Date
    Sep 2004
    Location
    Leuven, Belgium
    Posts
    1,853

    Default

    Another possibility I see is not storing the image data in a file but just keeping it in memory (in the model/request attribute) and have a specialized controller send it back to the browser. That way you avoid the file path problem and you also have no worries of doing cleanup of these image files.

    Erwin

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto
    Posts
    8

    Default

    ojolly,

    Thanks for the reply. However, i don't want to have spring API dependencies in the code. It also seems overly complex for something that im sure is quite common.

    ev9d9, you're right. I like that idea as well, but i still think there must be a simple way to do this...

    Thanks so far,
    jc

Similar Threads

  1. Replies: 5
    Last Post: Mar 17th, 2010, 04:32 AM
  2. Saving large files to a db using hibernate?
    By Dan Washusen in forum Data
    Replies: 10
    Last Post: Sep 20th, 2006, 12:18 PM
  3. Deferred file upload
    By Thomas Matzner in forum Web
    Replies: 4
    Last Post: Sep 30th, 2005, 12:06 PM
  4. Resolver for relative file paths?
    By NoWay in forum Container
    Replies: 3
    Last Post: Aug 16th, 2005, 03:46 AM
  5. Replies: 2
    Last Post: Nov 15th, 2004, 07:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •