@RequestMapping ("/act/worldcup_schedule_time/imgdownload") @ResponseBody public String scheduledownload (httpserv Letrequest request, HttpServletResponse response, HttpSession session) {response.setcharacterencoding ("UTF-8"); String downloadname = "Worldcup.jpg"; InputStream input = null; try {request.setcharacterencoding ("UTF-8"); Gets the path of the file//String URL = Session.getservletcontext (). Getrealpath ("/") + "Resources\\images\\act\\worldcup_merge \\worldcup720.png "; String URL = session.getservletcontext (). Getrealpath ("/") + "resources/images/act/worldcup_merge/worldcup720.png"; System.out.println (URL); File File = new file (URL); input = Fileutils.openinputstream (file); byte[] data = Ioutils.tobytearray (input); SYSTEM.OUT.PRINTLN ("FileName:" +downloadname); Response.reset (); Sets 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