首页 > 代码库 > IO流-----写到输出流

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

输出流:

   /**
     * 方法名: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流-----写到输出流