码迷,mamicode.com
首页 > 其他好文 > 详细

IO流-----写到输出流

时间:2016-12-29 20:06:25      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:ram   oid   chm   catch   class   param   except   方法   osi   

输出流:

   /**
     * 方法名:writeStream
     * 方法描述:写到输出流
     * @param title
     * @param wb
     * @param response
     * @param request
     * 返回值:void
     */
    private static void writeStream(String title, HSSFWorkbook wb, HttpServletResponse response, HttpServletRequest request){
        OutputStream os=null;
        try {
            String agent = request.getHeader("USER-AGENT");
            title.replaceAll("/", "-");
             // filename = new String(filename.getBytes("gbk"),"ISO8859_1");
             if (agent.toLowerCase().indexOf("firefox")>0) {
                title = new String(title.getBytes("utf-8"), "iso-8859-1");
             }else{
                title = URLEncoder.encode(title, "UTF-8");
             }
             response.reset();
             response.setCharacterEncoding("UTF-8");
             response.setHeader("Content-Disposition", "attachment; filename="+title);
             response.setContentType("application/vnd.ms-excel;charset=utf-8"); 
//             response.setContentType("application/octet-stream;charset=UTF-8");
             os = new BufferedOutputStream(response.getOutputStream());
             wb.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    } 

 

IO流-----写到输出流

标签:ram   oid   chm   catch   class   param   except   方法   osi   

原文地址:http://www.cnblogs.com/ldbangel/p/6233979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!