【发布时间】:2017-06-19 14:10:57
【问题描述】:
我有一个应用程序,用户可以在其中输入一些信息和图像,所以首先我将图像保存在数据库中,但我读到这不是一个好的做法,并且在部署应用程序时会产生成本。所以我想将图像保存在文件系统上,我读到我们不应该将它们保存在应用程序中,所以我想将它们保存在文件系统的外部文件夹中。我在文件夹 E:/images 中选择它们,效果很好。我的问题是我可以在生产应用程序中做什么来保存图像(不会有 E:/images),如何在部署应用程序的服务器上创建一个文件夹,以及如何在控制器中提及它而不是(E:/图像)。我正在使用 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";
}
【问题讨论】:
标签: spring file spring-mvc