File Download jsp code
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ page import =" java. io. file "%> <% @ page import =" java. io. outputStream "%> <% @ page import =" java. io. inputStream "%> <% @ page import =" java. io. fileInputStream "%> <% @ page import =" java.net. URLEncoder "%> <%
String path = application. getRealPath ("./images/a.jpg ");
File file = new File (path );
InputStream is = new FileInputStream (file );
Response. setHeader ("content-disposition", "attachment; filename =" + URLEncoder. encode (file. getName (), "UTF-8 "));
OutputStream OS = response. getOutputStream ();
Byte buffer [] = new byte [1024];
Int len;
While (len = is. read (buffer ))! =-1 ){
OS. write (buffer, 0, len );
}
OS. flush ();
OS. close ();
Is. close ();
%> L JSP running principle and nine implicit objects
L when each JSP page is accessed for the first time, the WEB Container will send the request to the JSP Engine (a Java program) for processing. The JSP Engine first translates JSP into a _ jspServlet (essentially a servlet) and calls it according to the servlet call method.
L because JSP is translated into servlet during the first access, the first access is usually slow. However, if the JSP Engine finds that JSP has not changed, it will not translate but directly call it, therefore, the execution efficiency of the program will not be affected.
L when the JSP Engine calls the _ jspServlet corresponding to JSP, it will pass or create nine web development-related objects for _ jspServlet. To facilitate developers to obtain the reference of these web objects when compiling JSP pages, the JSP technology designer specially defines nine corresponding variables, developers can use these variables on the JSP page to quickly obtain references to these nine objects.
L what are these nine objects and their functions are also the knowledge points frequently examined by the written examination.
L JSP nine implicit objects
L request
L response
L config
L application
L exception
L Session
L page
L out
L pageContext
L out implicit object
L The out implicit object is used to send text data to the client.
L The out object is returned by calling the getOut method of the pageContext object. Its function and usage are very similar to the PrintWriter object returned by the ServletResponse. getWriter method.
L The out implicit object type on the JSP page is JspWriter. JspWriter is equivalent to a PrintWriter with the cache function. You can set the buffer attribute of the page instruction of the JSP page to adjust its cache size, even disable its cache.
L The out object calls ServletResponse only when content is written to the out object and any of the following conditions are met. the getWriter method returns the PrintWriter object to write the content in the buffer zone of the out object to the buffer zone provided by the Servlet engine:
L set the buffer attribute of the page command to disable the cache function of the out object.
L The buffer of the out object is full.
L The entire JSP page ends
Author qiwancong