<!DOCTYPE html> <html> <head> <title>1.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <h1>文件下载列表文件</h1> <a href="/J2EE/download/cat.jpg">cat.jpg</a> <h1>手动编码的方式下载</h1> <a href="/J2EE/downloadservlet?filename=cat.jpg">cat.jpg</a> <!-- 问号表示给服务器传的参数 --> </body> </html>
<!DOCTYPE html> <html> <head> <title>1.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <h1>文件下载列表文件</h1> <a href="/J2EE/download/cat.jpg">cat.jpg</a> <h1>手动编码的方式下载</h1> <a href="/J2EE/downloadservlet?filename=cat.jpg">cat.jpg</a> <!-- 问号表示给服务器传的参数 --> </body> </html>package javapack; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class downloadservlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.接收参数 String filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"UTF-8");//通过html传来的参数获得filename,并且解决中文乱码问题 System.out.println(filename); //2.完成文件下载 //2.1设置Content-Type String type = this.getServletContext().getMimeType(filename);//获得文件类型 response.setHeader("Content-Type", type); //2.3设置文件的InputStream InputStream is = this.getServletContext().getResourceAsStream("/download/"+filename); //浏览器下载中文名文件,文件名乱码解决 String agent=request.getHeader("User-Agent");//获得浏览器版本 System.out.println(agent); if (agent.contains("FireFox")){ //比较复杂,不要求 } else{ filename=URLEncoder.encode(filename,"UTF-8"); } //2.2设置Content-Disposition头 response.setHeader("Content-Disposition","attachment;filename="+filename);//防止直接用浏览器打开,文件下载名为filename //获得response的输出流,把项目中的文件传给浏览器 ServletOutputStream sos = response.getOutputStream(); int len=0; byte[]b=new byte[1024]; while((len=is.read(b))!=-1){ sos.write(b,0,len); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet( reque补充,火狐浏览器解决中文乱码package javapack; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class output extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //test1(response); /* * 使用字符流输出中文 * 一定会乱码,response使用的字符流有缓冲区,编码是ISO-8859,不支持中文 */ response.setHeader("Content-Type", "text/html;charset=UTF-8");//设置浏览器默认打开的字符编码 response.setCharacterEncoding("UTF-8");//设置response的字符流缓冲区编码 //response.setContentType("UTF-8");//此一句话顶上面两句 response.getWriter().print("你好");;//把write方法用字符串输出 } private void test1(HttpServletResponse response) throws IOException { /* * 通过字节流向浏览器输出 */ response.getOutputStream().write("你好".getBytes("UTF-8"));//若getBytes()方法与浏览器输出显示编码一致,则不会乱码 response.setHeader("Content-Type", "text/html;charset=UTF-8");//设置浏览器打开格式 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }