java - the controller is saving the image but the image cannot be found in the desired folder ..is my controller correct -


@controller @requestmapping(value = "/admin/room")  public class roomcontroller { @autowired private roomservice roomservice;  @requestmapping(method = requestmethod.get) public string index(modelmap map) throws sqlexception {     map.addattribute("room", roomservice.getall());     return "admin/room/index"; }     @requestmapping(value = "/addroom", method = requestmethod.get)    public string addroom() throws sqlexception {     return "admin/room/addroom"; }   @requestmapping(value = "/editroom/{ro_id}", method = requestmethod.get) public modelandview edit(@pathvariable("ro_id") int ro_id) throws sqlexception {     modelandview mv = new modelandview("admin/room/editroom");     mv.addobject("room", roomservice.getbyid(ro_id));     return mv; }  @requestmapping(value = "/deleteroom/{ro_id}", method = requestmethod.get) public string delete(@pathvariable("ro_id") int ro_id) throws sqlexception {     roomservice.delete(ro_id);     return "redirect:/admin/room"; } 

this portion of code used saving image , other entities not able see image stored in desired folder

@requestmapping(value = "/save", method = requestmethod.post)  public string save(@requestparam("roomtype") string roomtype, @requestparam("roomdescription") string roomdescription, @requestparam("roomnumber") int roomnumber, @requestparam("file") multipartfile multipartfile, httpservletrequest req) throws sqlexception, ioexception {          room room = new room(); room.setroom_type(roomtype); room.setroom_description(roomdescription); room.setroom_number(roomnumber);      // : save room, fetch id of saved room , set through     // setter in above object.             if(room.getro_id()==0){      string serverrootpath = req.getservletcontext().getrealpath("");             system.out.println(serverrootpath);      // can change directory.     file roomimagedirectory = new file(serverrootpath + "d:\\hotels\\ploadedimages");      if (!roomimagedirectory.exists()) {         roomimagedirectory.mkdirs();     }     string[] filenametoken = multipartfile.getoriginalfilename().split("\\.");      // can change file name saved.     string newfilename = "room-" + room.getro_id() + "." + filenametoken[filenametoken.length - 1];      file roomimage = new file(roomimagedirectory, "/" + newfilename);     roomimage.createnewfile();     multipartfile.transferto(roomimage);             room.setimage(newfilename);             roomservice.insert(room);           }          else{         roomservice.update(room);         } return "redirect:/admin/room";   }     } 

according code, want save file under d:\\hotels\\ploadedimages. don't need use req.getservletcontext().getrealpath(""). change code

file roomimagedirectory = new file("d:\\hotels\\ploadedimages"); ... file roomimage = new file(roomimagedirectory, "/" + newfilename); 

i prefer file.separator instead of using / or \\


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -