Created: 6.19 & 6.24
1. Case - Download complete file
1) Under what circumstances would file download?
Browser can not parse the file to download
* Use a label directly to resources on the server
2) you need to write code to download the file on the server under what circumstances?
In theory, the browser can parse code you need to write the code to download the file
The actual development, as long as the files are downloaded to write the code to download the file
Download the essence file copy, copy the file from the server to the browser. So download the file IO technology will require server-side files InputStream to read, written in response buffer using ServletOutputStream
code show as below:
The code above pictures can be transmitted from the server to the browser, but the browser directly to resolve the picture displayed on the page, and not available for download, we need to set up two response header to inform the type and file browser file was opened.
1) inform the type of browser file: response.setContentType (MIME type of the file);
2) Open notices browser is to download the file:
response.setHeader("Content-Disposition","attachment;filename=文件名称");
code show as below:
* Client is not to distinguish types of files based on file extension, but (for extension carried out MIME mapping in web.xml tomcat's) by MIME type of the file
However, if you download the Chinese document, page while downloading occurs Chinese garbled or not display case file names, because different browsers, different from the default encoding to download files, ie is UTF-8 encoding, while Firefox is Base64 encoding. The need to address here in browser compatibility issues, solve the compatibility problem browser first task is to identify the visitor is ie or Firefox (other), can be distinguished by a property Http request body
Solve the garbage as follows (do not remember - understand):
Wherein the agent is the request header of User-Agent
1 if (agent.contains("MSIE")) { 2 3 // IE浏览器 4 5 filename = URLEncoder.encode(filename, "utf-8"); 6 7 filename = filename.replace("+", " "); 8 9 } else if (agent.contains("Firefox")) { 10 11 // 火狐浏览器 12 13 BASE64Encoder base64Encoder = new BASE64Encoder(); 14 15 filename = "=?utf-8?B?" 16 17 + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; 18 19 } else { 20 21 // 其它浏览器 22 23 filename = URLEncoder.encode(filename, "utf-8"); 24 25 }
完整代码:见WEB14代码DownloadServlet2.java
1 package com.itheima.content; 2 3 4 5 import java.io.FileInputStream; 6 7 import java.io.IOException; 8 9 import java.io.InputStream; 10 11 import java.net.URLEncoder; 12 13 14 15 import javax.servlet.ServletException; 16 17 import javax.servlet.ServletOutputStream; 18 19 import javax.servlet.http.HttpServlet; 20 21 import javax.servlet.http.HttpServletRequest; 22 23 import javax.servlet.http.HttpServletResponse; 24 25 26 27 import sun.misc.BASE64Encoder; 28 29 30 31 public class DownLoadServlet2 extends HttpServlet { 32 33 34 35 protected void doGet(HttpServletRequest request, HttpServletResponse response) 36 37 throws ServletException, IOException { 38 39 40 41 //*******文件名称是中文的下载******* 42 43 44 45 46 47 //获得要下载的文件的名称 48 49 String filename = request.getParameter("filename");//????.jpg 50 51 //解决获得中文参数的乱码----下节课讲 52 53 filename = new String(filename.getBytes("ISO8859-1"),"UTF-8");//美女.jpg 54 55 56 57 58 59 //获得请求头中的User-Agent 60 61 String agent = request.getHeader("User-Agent"); 62 63 //根据不同浏览器进行不同的编码 64 65 String filenameEncoder = ""; 66 67 if (agent.contains("MSIE")) { 68 69 // IE浏览器 70 71 filenameEncoder = URLEncoder.encode(filename, "utf-8"); 72 73 filenameEncoder = filenameEncoder.replace("+", " "); 74 75 } else if (agent.contains("Firefox")) { 76 77 // 火狐浏览器 78 79 BASE64Encoder base64Encoder = new BASE64Encoder(); 80 81 filenameEncoder = "=?utf-8?B?" 82 83 + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; 84 85 } else { 86 87 // 其它浏览器 88 89 filenameEncoder = URLEncoder.encode(filename, "utf-8"); 90 91 } 92 93 94 95 96 97 98 99 //要下载的这个文件的类型-----客户端通过文件的MIME类型去区分类型 100 101 response.setContentType(this.getServletContext().getMimeType(filename)); 102 103 //告诉客户端该文件不是直接解析 而是以附件形式打开(下载)----filename="+filename 客户端默认对名字进行解码 104 105 response.setHeader("Content-Disposition", "attachment;filename="+filenameEncoder); 106 107 108 109 //获取文件的绝对路径 110 111 String path = this.getServletContext().getRealPath("download/"+filename); 112 113 //获得该文件的输入流 114 115 InputStream in = new FileInputStream(path); 116 117 //获得输出流---通过response获得的输出流 用于向客户端写内容 118 119 ServletOutputStream out = response.getOutputStream(); 120 121 //文件拷贝的模板代码 122 123 int len = 0; 124 125 byte[] buffer = new byte[1024]; 126 127 while((len=in.read(buffer))>0){ 128 129 out.write(buffer, 0, len); 130 131 } 132 133 134 135 in.close(); 136 137 //out.close(); 138 139 140 141 } 142 143 144 145 protected void doPost(HttpServletRequest request, HttpServletResponse response) 146 147 throws ServletException, IOException { 148 149 doGet(request, response); 150 151 } 152 153 }
response细节点:
1)response获得的流不需要手动关闭,web容器(tomcat)会帮助我们关闭
2)getWriter和getOutputStream不能同时调用
验证码案例:不用掌握生成验证码,只要掌握html页面里怎么改