title: How to get Application path in spring MVC
body: <p>I am working in a spring mvc project which uploads an image and saves in the project folder which needs to be used in the project as .
  The image file successfully comes to the controller but I'm unable to save the file in WebContent directory.</p>
  
  <p>I would really appreciate help.
  Regards Hari.</p>
  
  <p>Item class</p>
  
  <pre><code>public class Item extends WebKinmelObject {
  private String name;
  private double price;
  private String manufacturer;
  private String description;
  private String category;
  private String imagePath;
  private int quantity;
  @Temporal(value=TemporalType.TIMESTAMP)
  private Date addedDate;
  
  public String getCategory() {
      return category;
  }
  public void setCategory(String category) {
      this.category = category;
  }
  public String getManufacturer() {
      return manufacturer;
  }
  public void setManufacturer(String manufacturer) {
      this.manufacturer = manufacturer;
  }
  public String getDescription() {
      return description;
  }
  public void setDescription(String description) {
      this.description = description;
  }
  private MultipartFile file;
  public MultipartFile getFile() {
      return file;
  }
  @Transient
  public void setFile(MultipartFile file) {
      this.file = file;
  }
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public double getPrice() {
      return price;
  }
  public void setPrice(double price) {
      this.price = price;
  }
  public String getImagePath() {
      return imagePath;
  }
  public void setImagePath(String imagePath) {
      this.imagePath = imagePath;
  }
  public int getQuantity() {
      return quantity;
  }
  public void setQuantity(int quantity) {
      this.quantity = quantity;
  }
  public Date getAddedDate() {
      return addedDate;
  }
  public void setAddedDate(Date addedDate) {
      this.addedDate = addedDate;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My controller goes like this.</p>
  
  <pre><code>@RequestMapping("admin/addItemAction")
  public ModelAndView addItemAction(@ModelAttribute("item")Item formItem,HttpServletRequest req){
      MultipartFile uploadedFile=formItem.getFile();
      if(uploadedFile!=null){
          String fileName=uploadedFile.getOriginalFilename();
          try {
              //-- if uploaded file is empty
              if(fileName!=""){
                  //String imagePath="/Users/hari/Documents/workspace/WebKinmel/WebContent/resources/upload/"+fileName;
                  String imagePath="/Users/hari/git/local_WebKinmel/WebKinmel/WebContent/resources/upload/"+fileName;
                  File f=new File(imagePath);
                  formItem.setImagePath("/WebKinmel"+imagePath.substring(imagePath.indexOf("/resources")));
                  formItem.setFile(null);
                  FileOutputStream fos=new FileOutputStream(f);
                  fos.write(uploadedFile.getBytes());
                  fos.flush();
                  fos.close();
                  f.createNewFile();
              }
          } catch (IOException e) {
              e.printStackTrace();
              System.out.println("==&gt;&gt;The uploaded file cound not be saved");
          }
      }
      if(formItem.isPersisted()){
  
          // to be done if no image
          String fileName=uploadedFile.getOriginalFilename();
          if(fileName==""){
              Item i=(Item) WebKinmelServiceManager.find(formItem.getId(), Item.class);
              formItem.setImagePath(i.getImagePath());//transferring old image path if no image path found
          }
          Date date=new Date();
          formItem.setAddedDate(date);
          WebKinmelServiceManager.update(formItem);
      }
      else{
          Date date=new Date();
          formItem.setAddedDate(date);
          WebKinmelServiceManager.save(formItem);
      }
      System.out.println("object"+formItem+" saved");
      ModelAndView mav=new ModelAndView("admin/adminItem");
      addItemContent(mav);
      mav.addObject("recent", formItem);
      return mav; 
  
  }
  </code></pre>
  
  <p>I want to save the image in WebContent directory rather than saving in 
  '/Users/hari/git/local_WebKinmel/WebKinmel/WebContent/resources/upload/' directory</p>
  
  <p>my servlet xml goes like this</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
      http://www.springframework.org/schema/beans     
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;
  
  &lt;mvc:annotation-driven /&gt;
  &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
  
  &lt;context:component-scan base-package="com.hari.controller" /&gt;
  
  &lt;bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="viewClass"
          value="org.springframework.web.servlet.view.JstlView"&gt;&lt;/property&gt;
      &lt;property name="prefix"&gt;
          &lt;value&gt;/WEB-INF/jsp/&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="suffix"&gt;
          &lt;value&gt;.jsp&lt;/value&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  &lt;bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/&gt;
  
  &lt;bean name="webkinmelProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="locations"&gt;
              &lt;value&gt;classpath*:webkinmel.properties&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>