Announcement Announcement Module
No announcement yet.
Best way to specify file paths in contexts Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:

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

    Many thanks,

  • #2
    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.



    • #3
      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.



      • #4

        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,