2017-06-17 97 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

檢查我的新答案 –

相關問題