我有一個應用程序,用戶可以在其中輸入一些信息和圖像,所以首先我將圖像保存在數據庫中,但是我讀到這不是一個好習慣, 。所以我想將圖像保存在文件系統中,我讀過它們不應該保存在應用程序中,所以我想將它們保存在文件系統的外部文件夾中。我選擇他們在文件夾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";
}
其實這是我的個人應用。你有一個例子如何做到這一點? –
檢查我的新答案 –