HttpServletResponse文件下载

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String realPath = "F:\\javaweb-03-servlet\\response\\target\\response\\WEB-INF\\classes\\1.jpg";
        System.out.println("下载文件的路径:" + realPath);
        // 2. 下载的文件名是啥?
        String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
        // 3. 设置想办法让浏览器能够支持(Content-Disposition)下载我们需要的东西,中文文件名URLEncoder.encode编码,否则有可能乱码
        resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        // 4. 获取下载文件的输入流
        FileInputStream in = new FileInputStream(realPath);
        // 5. 创建缓冲区
        int len = 0;
        byte[] buffer = new byte[1024];
        // 6. 获取OutputStream对象
        ServletOutputStream out = resp.getOutputStream();
        // 7. 将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区中的数据输出到客户端!
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }
<servlet>
        <servlet-name>filedownservlet-name>
        <servlet-class>com.zhang.servlet.FileServletservlet-class>
    servlet>

    <servlet-mapping>
        <servlet-name>filedownservlet-name>
        <url-pattern>/downurl-pattern>
    servlet-mapping>

HttpServletResponse文件下载_第1张图片
HttpServletResponse文件下载_第2张图片
HttpServletResponse文件下载_第3张图片

你可能感兴趣的:(JavaWeb)