Spring Java get webapp file path
@RequestMapping ("/act/worldcup_schedule_time/imgdownload") @ResponseBody public String Scheduledownload (httpservletrequest request, HttpServletResponse response, HttpSession session) { Response.setcharacterencoding ("UTF-8"); = "Worldcup.jpg"; NULL ; Try { request.setcharacterencoding ("UTF-8");
String osname = System.getproperties (). GetProperty ("Os.name");
String sp = (osname.equals ("window")? "\\" : "/");
//get the path to the file//String url = session.getservletcontext (). Getrealpath ("/") + "Resources\\images\\act\\worldcup_merge\\world Cup720.png ";String URL = session.getservletcontext (). Getrealpath ("/") + "Resources/images/act/worldcup_merge/worldcup720.png"; System.out.println (URL); File File=NewFile (URL); Input=fileutils.openinputstream (file); byte[] data =Ioutils.tobytearray (input); //System.out.println ("FileName:" +downloadname);Response.reset (); //set the header information for the response (Chinese problem resolution)Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode (downloadname, "UTF-8")); Response.AddHeader ("Content-length", "" "+data.length); Response.setcontenttype ("Image/png; Charset=utf-8 "); Ioutils.write (data, Response.getoutputstream ()); } Catch(Exception e) {logger.error ("Error downloading Picture"); if(Input! =NULL) {ioutils.closequietly (input); } } return NULL; }
Spring Java get webapp file path