I have written a code to upload the images(profile picture of an student) in the server running in linux environment.The code is shown below
@RequestMapping(value = "/updatePhoto",method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("id") String id,
@RequestParam("file") MultipartFile file,
HttpServletRequest request,
Model model) throws IOException {
if(!file.isEmpty())
{
try
{
String relativePath="/resources";
String absolutePath=request.getServletContext().getRealPath(relativePath);
System.out.print(absolutePath);
byte[] bytes=file.getBytes();
File dir=new File(absolutePath);
if(!dir.exists())
{
dir.mkdir();
}
File uploadFile=new File(dir.getAbsolutePath()+File.separator+id+".jpg");
BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream(uploadFile));
outputStream.write(bytes);
outputStream.close();
model.addAttribute("uploadMessage","image uploaded for id"+id);
}
catch (Exception e)
{
System.out.print(e);
}
}
return "successFileUpload";
}
i have stored in "/resources" folder.but the problem is, whenever i generate the war file of whole application and deploy in server, it flushes the "/resources" folder and deletes the old uploaded images.Is there any way or the path ,i could upload the images.
The way I do is:
/myImages
You are good to go now. I have read somewhere that you shouldn't save your stuff on /resources
folder because it makes your app independent from container you are using: with tomcat you could use catalina.home
but what if you shift to another container