File download does not require a third package is the file stream, byte input and output stream implementation.
<table> <tr> <td align= "right" > <a href= "download.jsp?path=<%= Getservletcontext (). Getrealpath ("123.png")%> "> Downloads </a></td> </tr> </table>
download.jsp
<%response.setcharacterencoding ("Utf-8"); String Pathname= Request.getparameter ("path"); Pathname=NewString (Pathname.getbytes ("Iso-8859-1")); File File=NewFile (pathname); InputStream ins=Newfileinputstream (file); OutputStream OS=Response.getoutputstream (); Bufferedinputstream bis=Newbufferedinputstream (INS); Bufferedoutputstream Bos=Newbufferedoutputstream (OS); Response.AddHeader ("Content-disposition", "attachment;filename=" +NewString (File.getname (). GetBytes ("Utf-8"), "Iso-8859-1")); Response.AddHeader ("Content-length", file.length () + ""); Response.setcontenttype ("Application/octet-stream"); intLen = 0; byte[] Bys =New byte[1024]; while((Len=ins.read (bys))!=-1) {bos.write (bys); Bos.flush (); } out.clear (); out=Pagecontext.pushbody (); Bos.close (); Bis.close (); %>
The Out.clear () and Pagecontext.pushbody () methods are called in the preceding code because they are built into the JSP page, and then use the Response.getoutstream () The java.lang.IllegalStateException exception is thrown by the method, but it is also true that I removed both lines from the code above.
Java Download file