当前位置:   article > 正文

如何用Java实现浏览器文件下载功能_java浏览器下载文件

java浏览器下载文件

一、了解ServletContext对象

代表整个web应用,可以和程序的容器(服务器)来通信

1. 概念

        ServletContext是Servlet中最大的一个接口,呈现了web应用的Servlet视图。服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象。这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象

2. 获取

1. 通过request对象获取

request.getServletContext();

2. 通过HttpServlet获取

this.getServletContext();

3. 测试

  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import javax.servlet.annotation.*;
  4. import java.io.IOException;
  5. @WebServlet(name = "servletContextDemo1", value = "/servletContextDemo1")
  6. public class servletContextDemo1 extends HttpServlet {
  7. @Override
  8. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  9. ServletContext context1 = request.getServletContext();
  10. ServletContext context2 = this.getServletContext(); //httpServlet下的servletContext
  11. System.out.println(context1);
  12. System.out.println(context2);
  13. }
  14. @Override
  15. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. this.doGet(request,response);
  17. }
  18. }

 3. 功能

1. 获取MIME类型

  • MIME类型:在互联网通信过程中定义的一种文件数据类型
  • 格式: 大类型/小类型 text/html image/jpeg
  • 获取:String getMimeType(String file)
  1. @WebServlet(name = "servletContextDemo2", value = "/servletContextDemo2")
  2. public class servletContextDemo2 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5. ServletContext context = this.getServletContext(); //httpServlet下的servletContext
  6. String filename = "a.jpg";
  7. String mimeType = context.getMimeType(filename); //获取mime类型
  8. System.out.println(mimeType); //mime类型为 image/jpeg
  9. }
  10. @Override
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. this.doGet(request,response);
  13. }
  14. }

2. 域对象:共享数据

  • 设置数据域:setAttribute(String name,Object value)
  1. @WebServlet(name = "servletContextDemo3", value = "/servletContextDemo3")
  2. public class servletContextDemo3 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5. ServletContext context = this.getServletContext(); //httpServlet下的servletContext
  6. context.setAttribute("msg","ohmyga"); //设置共享数据,给整个服务器共享
  7. }
  8. @Override
  9. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  10. this.doGet(request,response);
  11. }
  12. }
  • 获取数据域:getAttribute(String name)
  1. @WebServlet(name = "servletContextDemo4", value = "/servletContextDemo4")
  2. public class servletContextDemo4 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5. ServletContext context = this.getServletContext(); //httpServlet下的servletContext
  6. Object msg = context.getAttribute("msg");//设置共享数据
  7. System.out.println(msg);
  8. }
  9. @Override
  10. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. this.doGet(request,response);
  12. }
  13. }

 二、如何获取文件真实路径

String getRealPath(String path);

1.  web目录下资源访问

  1. String a = context.getRealPath("/a.txt");
  2. System.out.println(a);

2. WEB-INF目录下的资源访问

  1. String b = context.getRealPath("/WEB-INF/b.txt");
  2. System.out.println(b);

3. src目录下的资源访问

  1. String c = context.getRealPath("/WEB-INF/classes/c.txt");
  2. System.out.println(c);
  1. @WebServlet(name = "servletContextDemo5", value = "/servletContextDemo5")
  2. public class 获取文件真实路径 extends HttpServlet {
  3. @Override
  4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  5. ServletContext context = this.getServletContext(); //httpServlet下的servletContext
  6. String a = context.getRealPath("/WEB-INF/classes/a.txt"); //src目录下的a.txt
  7. System.out.println(a);
  8. }
  9. @Override
  10. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. this.doGet(request,response);
  12. }
  13. }

 三、实现文件下载

1. html文件

        1. 编写一段简单的html代码,比如下载这个 1.jpg 文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>下载</title>
  6. </head>
  7. <body>
  8. <a href="/Web/servletContextDemo6?filename=1.jpg">图片下载</a>
  9. </body>
  10. </html>

         2. 预览效果

 2. web 服务器代码

这里注意需要设置response响应头的mime类型响应头的打开方式!!!

response.setHeader(String name, String value);

  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import javax.servlet.annotation.*;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. @WebServlet(name = "servletContextDemo6", value = "/servletContextDemo6")
  7. public class 文件下载 extends HttpServlet {
  8. @Override
  9. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  10. String filename = request.getParameter("filename"); //获取请求参数
  11. //使用字节输入流加载文件进内存
  12. ServletContext servletContext = this.getServletContext();
  13. String realPath = servletContext.getRealPath("/WEB-INF/classes/servletContext/" + filename); //找到文件的服务器路径
  14. FileInputStream fis = new FileInputStream(realPath); //使用字节输入流读取文件
  15. //设置response响应头
  16. String mimeType = servletContext.getMimeType(filename); //获取mime类型
  17. response.setHeader("content-type",mimeType); //设置响应头类型
  18. response.setHeader("content-disposition","attachment;filename=" + filename); //设置响应头打开方式
  19. //将输入流的事件写出到输出事件
  20. ServletOutputStream sos = response.getOutputStream();
  21. byte[] buffer = new byte[1024 * 8];
  22. int length = 0;
  23. while((length = fis.read(buffer)) != -1){
  24. sos.write(buffer,0,length);
  25. }
  26. sos.close();
  27. }
  28. @Override
  29. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  30. this.doGet(request,response);
  31. }
  32. }

 3. 预览效果

点击下载

 文件就被默认(也可以用浏览器设置到其他地方)的下载到了这个位置

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/63560
推荐阅读
相关标签
  

闽ICP备14008679号