2017-06-17 89 views
0

我有一个应用程序,用户可以在其中输入一些信息和图像,所以首先我将图像保存在数据库中,但是我读到这不是一个好习惯, 。所以我想将图像保存在文件系统中,我读过它们不应该保存在应用程序中,所以我想将它们保存在文件系统的外部文件夹中。我选择他们在文件夹E:/图像,它工作正常。我的问题是我可以在生产应用程序中做什么来保存图像(不会有E:/图像),我如何在部署应用程序的服务器上创建文件夹,以及如何在控制器中提及它,而不是(E:/图像)。我使用Spring MVC。 这里我控制器:将用户的图像保存在文件系统的文件夹中Spring MVC

@RequestMapping(value="/save",method=RequestMethod.POST) 
    public String add (@RequestParam("prix") Long prix, 
      RequestParam("adresse") String ville, 
      @RequestParam("categorie") String categorie, 
      @RequestParam("photos") MultipartFile file, 
      ) throws FileNotFoundException 


{ 

    String chemin=null; 
    if (!file.isEmpty()) 
    { 
     try { 
     String orgName = file.getOriginalFilename(); 
     // this line to retreive just file name 
     String 
     name=orgName.substring(orgName.lastIndexOf("\\")+1,orgName.length()); 
     chemin="e:\\images\\"+name; 
     File file1=new File(chemin); 
     file.transferTo(file1); 
     } catch (IOException e) { 
            e.printStackTrace(); 
            } 

    } 
    annonce.setImage(chemin); 
    annonce.setTitre(prix); 
    annonce.setCorps(ville); 
    annonce.setPrix(cetegorie) 
    annoncedao.save(annonce); 
    return "SuccessAddAnnonce"; 
} 

回答

0

在你的类路径中添加somename.property文件。下面添加行吧:

imagepath=e:\\images; 

现在您为属性文件豆在下面的Spring配置文件。

<bean id="projectProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="locations"> 
     <list> 
      <value>classpath*:somename.properties</value> 
     </list> 
    </property> 
</bean> 

现在在你的控制器,添加Bean:

@Resource(name="projectProperties") 
    private Properties projectProperties; 

现在更改订单:

chemin=projectPropertiesProperties.getProperty("imagepath")+name; 
0

不要去在代码中硬编码文件路径。使用属性文件并将其交给生产团队。因此,无论何时在生产环境中部署应用程序,只需在属性文件中更改要保存文件的文件路径即可。

+0

其实这是我的个人应用。你有一个例子如何做到这一点? –

+0

检查我的新答案 –

相关问题