webLogic环境下,导出excel2007打不开问题

之前的项目一直运行到tomcat7环境中,由于客户要求使用webLogic服务器,项目部署到webLogic环境中,遇到很多问题,其中导出报表问题涉及范围比较多,本人花了两天时间一直研究,查资料,终于解决在webLogic环境下,导出excel表格问题。以下是关键性代码:




        // 设置response的Header
        PrintWriter writer = null;
        try
        {
            response.setContentType("octets/stream");
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("gbk"), "iso-8859-1"));
            response.setContentLength(new Long(file.length()).intValue());
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        response.setCharacterEncoding("utf-8");
        InputStream inputStream = null;
        OutputStream os = null;
        try
        {
            try
            {
                inputStream = new FileInputStream(file);
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
                writer = response.getWriter();
                writer.write(JSON.toJSONString("下载文件出错"));
                writer.flush();
                // throw new RoxException("下载文件出错");
            }
            
            os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0)
            {
                os.write(b, 0, length);
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            // throw new RoxException("下载文件出错");
        }
        catch (IOException e)
        {
            e.printStackTrace();
            // throw new RoxException("下载文件出错");
        }
        finally
        {
            if (writer != null)
            {
                writer.close();
            }
            if (os != null)
            {
                try
                {
                    os.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (inputStream != null)
            {
                try
                {
                    inputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    

关键性代码中,其中response.setContentLength(new Long(file.length()).intValue());代码是关键代码,如果不指定下载文件大小,在导出文件,使用excel2007打开,会提示“文件格式错误”,指定文件大小之后,在使用excel2007就可以正常打开了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值