我开发了一个应用程序,用户可以在其中提供文本信息和图像。我想将图像保存在文件系统和数据库中,我将添加每个用户的链接到它的图像因此,我需要将用户ID添加到图像名称,以便不会获得具有相同名称的图像。我使用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; //here I want to add id (auto generated)
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";
}
,但我不能获得ID女巫产生的,所以我不能把它扔@RequestParam像ADRESS或categorie,因为它是自动生成的汽车,我还是不要吨有它,直到我们呼叫保存方法巫婆是最后的。 欢迎任何建议。
你想为用户生成一个随机ID? – abstractnature
不,每次我添加一个用户一个ID生成,我想从数据库 –
得到最后一个ID,你没有告诉它是哪个数据库,mysql?并在其架构是什么,所以我可以帮助 – abstractnature