file 附件形式下载文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wanggeying/article/details/82887333

        // 以流的形式下载文件。
        File f = new File(path);
        if (!f.exists()) {
            response.sendError(404, "File not found!");
            return;
        }
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
        byte[] buf = new byte[1024];
        int len = 0;
        response.reset(); 
        response.setContentType("application/x-msdownload");
        String filename=URLEncoder.encode(map.get("mm")+"知识系统访问量统计月报报表.doc","UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="+filename);
        OutputStream out = response.getOutputStream();
        while ((len = br.read(buf)) > 0)
            out.write(buf, 0, len);
        br.close();
        out.close();

猜你喜欢

转载自blog.csdn.net/wanggeying/article/details/82887333