ssm项目中实现文件下载功能的源代码

package com.lsgjzhuwei.servlet.response;

  

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

  

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

  

/**

 * Servlet implementation class ServletDownload

 */

@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })

public class ServletDownload extends HttpServlet {

  private static final long serialVersionUID = 1L;

      

  /**

   * @see HttpServlet#HttpServlet()

   */

  public ServletDownload() {

    super();

    // TODO Auto-generated constructor stub

  }

  

  /**

   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

   */

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // TODO Auto-generated method stub

      

    //获得请求文件名

    String filename = request.getParameter("filename");

    System.out.println(filename);

      

    //设置文件MIME类型

    response.setContentType(getServletContext().getMimeType(filename));

    //设置Content-Disposition

    response.setHeader("Content-Disposition", "attachment;filename="+filename);

    //读取目标文件,通过response将目标文件写到客户端

    //获取目标文件的绝对路径

    String fullFileName = getServletContext().getRealPath("/download/" + filename);

    //System.out.println(fullFileName);

    //读取文件

    InputStream in = new FileInputStream(fullFileName);

    OutputStream out = response.getOutputStream();

      

    //写文件

    int b;

    while((b=in.read())!= -1)

    {

      out.write(b);

    }

      

    in.close();

    out.close();

  }

  

  /**

   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

   */

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // TODO Auto-generated method stub

  }

  

}

重启tomcat服务器,即可实现对压缩包和对图片的下载。

ssm项目中实现文件下载功能的源代码_第1张图片

ssm项目中实现文件下载功能的源代码_第2张图片

 

三、小技巧:
点击链接来下载文件的方式很简便,后台把文件流输出来,通过浏览器实现下载功能,包括询问位置与文件存放,大多数浏览器会配置一个固定位置,不一定每次都问。

前端就非常简单了,一个标签,href=“后台方法地址”,如果你的需求不能直接用超链接方式,可以在js里写 window.location.href =“后台方法地址”。

这样跳转到后台方法后

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

String filePath = this.getClass().getClassLoader().getResource("").toURI().getPath()

          + "/exportPdf.pdf"; //文件在项目中的路径

    File outfile = new File(filePath);

    String filename = outfile.getName();// 获取文件名称

    InputStream fis = new BufferedInputStream(new FileInputStream(

          filePath));

    byte[] buffer = new byte[fis.available()];

    fis.read(buffer); //读取文件流

    fis.close();

    response.reset(); //重置结果集

    response.addHeader("Content-Disposition", "attachment;filename="

        + new String(filename.replaceAll(" ", "").getBytes("utf-8"),

        "iso8859-1")); //返回头 文件名

    response.addHeader("Content-Length", "" + outfile.length()); //返回头 文件大小

    response.setContentType("application/octet-stream");  //设置数据种类

    //获取返回体输出权

    OutputStream os = new BufferedOutputStream(response.getOutputStream());

    os.write(buffer); // 输出文件

    os.flush();

    os.close();

你可能感兴趣的:(文件下载)