2015-12-06 59 views
0

我在一个spring mvc项目中工作,该项目上传一个图像并保存在需要在项目中使用的项目文件夹中。 图像文件成功进入控制器,但我无法将文件保存在WebContent目录中。如何在spring中获取应用程序路径MVC

我真的很感激帮助。 关心哈里。

Item类

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; 
} 

}

我的控制器是这样的。

@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("==>>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; 

} 

我想保存在WebContent目录的图像,而不是 节约 '/用户/哈利/混帐/ local_WebKinmel/WebKinmel /的WebContent /资源/上传/' 目录

我的servlet的XML是这样这

<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"> 

<mvc:annotation-driven /> 
<mvc:resources mapping="/resources/**" location="/resources/" /> 

<context:component-scan base-package="com.hari.controller" /> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView"></property> 
    <property name="prefix"> 
     <value>/WEB-INF/jsp/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 

</bean> 
<bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> 

<bean name="webkinmelProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
      <value>classpath*:webkinmel.properties</value> 
    </property> 
</bean> 

+0

Plz发布代码 – Rehman

回答

1

您可以使用ServletContext#getRealPath()就要上SERV部署/展开的WAR文件夹结构的路径呃文件系统。 尽管在爆炸战争中保存上传的图像是不建议。你应该改用专用的外部文件夹。

+0

包甚至可能不会爆炸。 – chrylis

+0

@chrylis - 是的,你是对的,尽管大多数容器确实创建了爆炸结构,但不能依赖,所以在运行时不要向战争内容添加任何东西总是正确的。 – Shailendra

+0

你的意思是==> ServletContext。getRealPath(),不要以为ServletContext类有getRealPath()方法,请您详细说明:) –

1

由于Shailendra表示,您可以使用ServletContext获取您的上下文的真实路径。最简单的获取ServletContext的方法是在控制器类中自动运行它。例如:

@Controller 
    @RequestMapping("/") 
    public class ExampleController{ 

     @Autowired 
     ServletContext context; 

    } 

之后,你可以使用控制器的方法context.getRealPath("/")让您的应用上下文的根路径。但是,再次如Shailendra所述,使用专用的外部文件夹而不是爆炸的战争文件夹会更好。

,我建议你在你加属性webkinmel.properties文件与期望的路径:

webcontent.path=/Users/hari/git/local_WebKinmel/WebKinmel/ 

而且在你与春天注入控制装置使用该属性:

@Controller 
public class ControllerClass{ 

    @Value("${webcontent.path}") 
    private String webcontentPath; 

    ... 
} 

或者另一种方式是在配置通过财产xml:

<bean name="myController" class="ControllerClass"> 
    <property name="webcontentPath" value="${webcontent.path}"/> 
</bean> 

使文件夹可以从眉毛访问呃,只是添加在你的配置XML:

<mvc:resources mapping="/web/**" location="file:${webcontent.path}"/> 

比如你保存文件hello.png/Users/hari/git/local_WebKinmel/WebKinmel/。它可以通过url访问http://yourhost:8080/web/hello.png

+0

谢谢shailendra,但因为我使用eclipse进行开发,所以它给了我输出路径。 “/Users/hari/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebKinmel/” 而不是我需要的路径类似于 “/ Users/hari/git/local_WebKinmel/WebKinmel /“ 任何想法:) ?? –

+0

您能否介绍一下webkinmel.properties文件。我对此不太了解。 –

+0

根据您的servlet.xml,您在项目的类路径中有_webkinmel.properties_。该文件应该包含属性,如_name = value_。您可以将任何属性添加到此文件中,并将其存入控制器类中。 –

相关问题