Java中关于Servlet中请求中文乱码及文件下载
1,Servlet请求响应中文乱码问题
1 package com.demo.servlet; 2 import java.io.PrintWriter; 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class Demo5Servlet extends HttpServlet { 11 12 @Override 13 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 /* 15 * 关于重定向,Servlet封装了sendRedirect方法 则sendRedirect方法的底层其实是两步 16 * 第一步是设置响应状态码为302 17 * 第二步是设置响应头的location属性值为需要跳转到的url 18 */ 19 resp.setStatus(302); 20 resp.setHeader("location", "/ServletDemo/demo2"); 21 // 以上两步的效果与以下方法相同 22 resp.sendRedirect("/ServletDemo/demo2"); 23 // 设置页面自动过5秒种跳转到另一个地址 24 resp.setHeader("refresh", "5;url=/ServletDemo/demo2"); 25 /* 26 * Servlet响应中文乱码的问题,主要分两步 27 * 第一步是当response把中文写入到Tomcat引擎之前,是先把内容写入到response缓冲区的, 28 * 此时需要告诉response需要以什么样的编码形式写入到缓冲区,即设置setCharacterEncoding(); 29 * 第二步是告诉浏览器以什么样的编码方式读取response响应的信息,即设置setHeader 30 * 其实第一步不设置,只设置第二步便可起到同样的效果 31 */ 32 //告诉response缓冲区将以什么样的编码方式写入 33 resp.setCharacterEncoding("utf-8"); 34 //行文浏览器以什么样的编码方式解决response响应的内容 35 resp.setHeader("Content-type", "text/html;charset=utf-8"); 36 PrintWriter writer = resp.getWriter(); 37 writer.write("hello"); 38 writer.write("<br/>"); 39 writer.write("你好"); 40 41 } 42 43 @Override 44 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 45 46 doGet(req, resp); 47 } 48 49 }
2,Servlet手动实现下载
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 <script type="text/javascript"> 7 function click() { 8 var href = '/ServletDemo/down?filename=张三.jpg'; 9 href = encodeURI(href); 10 console.info(href); 11 location.href = href; 12 } 13 </script> 14 </head> 15 <body> 16 <a href="/ServletDemo/down?filename=a.jpg">jpg</a> 17 <br /> 18 <a href="/ServletDemo/down?filename=a.mp3">mp3</a> 19 <br /> 20 <a href="/ServletDemo/down?filename=a.flv">flv</a> 21 <br /> 22 <a href="/ServletDemo/down?filename=a.mp4">mp4</a> 23 <br /> 24 <a href="/ServletDemo/down?filename=a.txt">txt</a> 25 <br /> 26 <a href="/ServletDemo/down?filename=a.zip">zip</a> 27 <br /> 28 <a href="javascript:click()">张三</a> 29 30 </body> 31 </html>
1 package com.demo.servlet; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.net.URLEncoder; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletOutputStream; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import sun.misc.BASE64Encoder; 15 16 public class DownServlet extends HttpServlet { 17 18 @Override 19 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 20 String filename = req.getParameter("filename");// 获取所需要下载的文件名 21 String oldFileName = filename;// 之所以声明一个oldFileName是为了服务器需要根据这个文件名字找到相对应的文件 22 System.out.println(filename); 23 String agent = req.getHeader("User-Agent");// 获取客户端浏览器版本 24 //根据浏览器版本对所要下载的文件名称进行编码,以便在下载的时候正确显示文件名称 25 if (agent.contains("MSIE")) { 26 // IE浏览器 27 filename = URLEncoder.encode(filename, "utf-8"); 28 filename = filename.replace("+", " "); 29 } else if (agent.contains("Firefox")) { 30 // 火狐浏览器 31 BASE64Encoder base64Encoder = new BASE64Encoder(); 32 filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; 33 } else { 34 // 其它浏览器 35 filename = URLEncoder.encode(filename, "utf-8"); 36 } 37 String path = this.getServletContext().getRealPath("download/" + oldFileName);// 获取所下载的资源的绝对路径 38 // 设置要下载的文件类型,通过文件名获取文件的MIME类型 39 resp.setContentType(this.getServletContext().getMimeType(filename)); 40 // 告诉浏览器别直接打开文件,而是以下载的形式保存文件 41 resp.setHeader("Content-Disposition", "attachment;filename=" + filename); 42 InputStream input = new FileInputStream(path);// 将文件封装输入流 43 ServletOutputStream output = resp.getOutputStream();// 获取response的输入流对象 44 int len = 0;// 读取到的字节长度 45 byte[] buffer = new byte[1024];// 读取到的缓冲区 46 while ((len = input.read(buffer)) > 0) { 47 output.write(buffer, 0, len);// 读取缓冲区的字节到输出流 48 } 49 input.close(); 50 output.close(); 51 52 } 53 54 @Override 55 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 56 57 doGet(req, resp); 58 } 59 60 }