zoukankan      html  css  js  c++  java
  • servlet下载文件(五)

    1.servlet下载文件

      servlet下载文件就是将服务器端的文件传输到客户端。

    2案例

    下载文件servlet类

    package com.learn;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    /**
     * Created by Administrator on 2017/09/24.
     */
    public class DownLoadServlet extends HttpServlet {
        private String filePath;
    
        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            filePath = config.getInitParameter("filePath");
            //初始化路径
            //filePath = config.getServletContext().getRealPath(filePath);
            System.out.println("初始化文件路径:"+filePath);
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req,resp);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            OutputStream out; //输出流
            InputStream in; //输入流
    
            String fileName = req.getParameter("fileName");
            System.out.println("文件名称:"+fileName);
            //如果把文件名为null则提示用户
            if(fileName == null){
                out = resp.getOutputStream();
                out.write("please input fileName".getBytes());
                out.close();
            }
            //获取文件流
            in = getServletContext().getResourceAsStream(filePath+ File.separator+fileName);
            System.out.println(in==null?true:false);
            int length = in.available();
            
            //设置返回消息头部信息
            resp.setContentType("application/force-download");
            resp.setHeader("Content-Length",String.valueOf(length));
            resp.setHeader("content-disposition","attachment;filename=""+fileName+""");
            //输出文件到客户端
            out = resp.getOutputStream();
            int bytesend = 0 ;
            byte[] buff = new byte[512];
            while ((bytesend = in.read(buff))!= -1){
                out.write(buff,0,bytesend);
            }
            in.close();
            out.close();
        }
    }

    web.xml配置

      <servlet>
        <servlet-name>download</servlet-name>
        <servlet-class>com.learn.DownLoadServlet</servlet-class>
        <init-param>
            <param-name>filePath</param-name>
            <param-value>file</param-value>
        </init-param>
     </servlet>
     <servlet-mapping>
        <servlet-name>download</servlet-name>
        <url-pattern>/download</url-pattern>
     </servlet-mapping>
  • 相关阅读:
    CFileDialog打开多个文件失败 返回错误 FNERR_BUFFERTOOSMALL
    VC 控件集合
    [摘]思科认证三步走及找工作的七大职业走向
    windows无法配置此无线连接解决办法
    VS 2019 项目添加引用,提示:对COM组件的调用返回了错误HRESULT E_FAIL
    c# winform 获取当前程序运行根目录
    DataGridView 转换成 DataTable
    SQL Server日志文件过大 大日志文件清理方法 不分离数据库
    行动吧!让自己骚起来
    抖音很火的3D旋转相册 (源代码分享)
  • 原文地址:https://www.cnblogs.com/wwyx-xi/p/7588503.html
Copyright © 2011-2022 走看看