A
The easiest way is to do hyperlinks on the Web page, such as: <a href= "Music/abc.mp3" > click Download </a>.
However, the directory resources on the server will be exposed directly to the end user, which will bring some unsafe factors to the website.
So there are other ways to download it, and you can use:
1, the way of RequestDispatcher;
2, the use of file flow output mode download. Recommended
1, the use of RequestDispatcher way to carry out
JSP Code <% response.setcontenttype ("Application/x-download"); Set to download application/x-download String filedownload = "/file name to download";/ The relative path of the file to be downloaded String filedisplay = the "save file name to be displayed to the user eventually";//File save names displayed when downloading files string filenamedisplay = urlencoder.encode (Filedisplay, "UTF-8"); response.addheader ("Content-disposition", "attachment;filename=" + filedisplay); try { Requestdispatcher dis = application.getrequestdispatcher (filedownload); if (dis!= null) { dis.forward (request,response); } response.flushbuffer (); } catch (exception e) { e.printstacktrace (); } finally { } %>
2, the use of file flow output mode download
JSP code <% @page language= "java" contenttype= "Application/x-msdownload" pageencoding = "gb2312"%> <% //processing: with file stream output in the case of file downloads // Add Response.reset (), and do not wrap all%> behind, including the last; response.reset (), or you can add or not response.setcontenttype ("Application/x-download"); // Application.getrealpath ("/main/mvplayer/capsetup.msi"); physical path acquired string filedownload = "Think of ways to find the physical path to download files + filename"; String filedisplay = "to provide users with the download file name"; string filedisplay = urlencoder.encode (Filedisplay, "UTF-8"); response.addheader ("Content-disposition", "attachment;filename=" + filedisplay); java.io.OutputStream outp = null; Java.io.fileinputstream in = null; try { outp = response.getoutputstream (); in = new fileinputstream ( Filenamedownload); byte[] b = new byte[1024]; int i = 0; while (I = in.read (b) ) > 0 { outp.write (b, 0, i); } // Outp.flush (); //To be the next two words, or you will be the error // Java.lang.illegalstateexception: getoutputstream () has already been called for //this response out.clear (); out = pagecontext.pushbody (); } catch (exception
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.