我有一个应用程序,用户可以在其中输入一些信息和图像,所以首先我将图像保存在数据库中,但是我读到这不是一个好习惯, 。所以我想将图像保存在文件系统中,我读过它们不应该保存在应用程序中,所以我想将它们保存在文件系统的外部文件夹中。我选择他们在文件夹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";
}
其实这是我的个人应用。你有一个例子如何做到这一点? –
检查我的新答案 –