java实现通过浏览器下载文件


 

OutputStream output;
        try {
            output = response.getOutputStream();
            //清空缓存
            response.reset();
            //定义浏览器响应表头,顺带定义下载名,比如students(中文名需要转义)
            String s = file.getName() + file.getBirthday();
            response.setHeader("Content-disposition", "attachment;filename=" + new String(s.getBytes(), "iso-8859-1") + ".xls");
            //定义下载的类型,标明是excel文件
            response.setContentType("application/vnd.ms-excel");
            //这时候把创建好的excel写入到输出流
            wb.write(output);
            //养成好习惯,出门记得随手关门
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM