public void filedownload (HttpServletResponse response) throws Exception {
ServletContext context = this.getServletContext();
String path = context.getRealPath("/download/awf.jpg");
String filename = path.substring(path.lastIndexOf("//") + 1);
// If the downloaded file is a Chinese file, the file name needs to be URL encoded;
response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename, "UTF-8"));
InputStream in = new FileInputStream(path);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
BeanUtils uses :
BeanUtils.pupulate(bean,MapInstance);//Load the bean with map, which contains the key corresponding to the bean attribute and the value corresponding to the key;
BeanUtils.copyProperties(bean,MapInstance);//Copy map to bean;
Forwarding is a request, using the same response and request;
Page jump:
<1>String message = "<meta http-equiv='refresh' content='3;url=/webTwo/index.jsp'><a href='webTwo/index.jsp'>AAAA</a>";
this.getServletContext().setAttribute("message", message);
this.getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);//Bring the message to the message page for display;
<2>response.setHeader("refresh", "3;url='/webTwo/index.jsp'");
response.getWriter().write("Congratulations on successful login. If there is no transfer, please click the hyperlink <a href='webTwo/index.jsp'>AAAA</a>");
Program code:
//What code table the program uses to output must control what code table the browser uses to open;
// Use meta technology in HTML to simulate http response headers to control browser behavior;
// out.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());
response.setCharacterEncoding("UTF-8");//Set the code table used by the response and control the code table used by the response to write data to the browser;
response.setHeader("Content-type", "text/html;charset=UTF-8");//Specify what code table the browser uses to open the data;
// Equivalent to the above two sentences:
// response.setContentType("text/html;charset=UTF-8");
Response.setDateHeader("expires",System.currentTimeMillis() + 1000*3600);//Set the session validity time to 10 minutes;
Response.getWriter().write(data); Response.getWriter() --> return PrintWriter;
Response.setHeader("refresh","3");