Can be downloaded across domains
/** * Audio Download */@RequestMapping ("/audio/download") public void DownloadFile (HttpServletRequest request , httpservletresponse response) throws IOException {//string id = request.getparameter ("id"); String FilePath = servletrequestutils.getstringparameter (Request, "FilePath", "" "); String fileName = servletrequestutils.getstringparameter (Request, "FileName", ""); String Desturl = FilePath; System.out.println ("--------------" +filepath); String fileformat=filepath.substring (Filepath.lastindexof (".")); String Name=filename.trim () +fileformat; File F = new file (FilePath); Response.setheader ("Content-disposition", "attachment; Filename= "+java.net.urlencoder.encode (F.getname ()," UTF-8 ")); System.out.println ("--------------" +f.getname ()); Establish link URL url = new URL (desturl); HttpURLConnection Httpurl = (httpurlconnection) url.openconnection (); ConnectionThe specified resource Httpurl.connect (); Get the network input stream Bufferedinputstream bis = new Bufferedinputstream (Httpurl.getinputstream ()); Response.setcontenttype ("Application/x-msdownload"); Response.setheader ("Content-disposition", "attachment; Filename= "+java.net.urlencoder.encode (name," UTF-8 ")); OutputStream out = Response.getoutputstream (); byte[] buf = new byte[1024]; if (Desturl! = null) {Bufferedinputstream br = bis; int len = 0; while (len = Br.read (buf)) > 0) {out.write (buf, 0, Len); } br.close (); } out.flush (); Out.close (); }
Page main code:
<div class= "txt-2" style= "Padding-left:6px;float:none;" > <a href= "/wxj/audio/download?filepath=${(contentjson.audiolink)!" &filename=${(Course.title)! "}" class= "Btn btn-info" > Downloads </a></div>
Springmvc Download File