神雕摩卡之2
2015-01-05 07:28
采纳率: 0%
浏览 2.9k

java实现流下载为什么没有响应???急急急

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

    String filename = "a" + df.format(new Date()) + ".xls";
    //File file=new File(filename);
    try {
        ServletOutputStream os = response.getOutputStream();  
        response.reset();  
        // 设置response的Header 
         response.setContentType("application/x-msdownload"); 
         response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));  
         //response.addHeader("Content-Length", "" + file.length());  

        WritableWorkbook wb=Workbook.createWorkbook(os);
        int count=list.size()/65535+1;
        for(int i=1;i<=count;i++){

        WritableSheet ws=wb.createSheet("sheet"+String.valueOf(i), i-1);
        ws.addCell(new Label(0, 0, "标题"));
        ws.addCell(new Label(1, 0, "姓名"));
        ws.addCell(new Label(2, 0, "手机号"));
        ws.addCell(new Label(3, 0, "咨询编号"));
        ws.addCell(new Label(4, 0, "内容"));
        //ws.addCell(new Label(0, 5, "回复内容"));

        int row=1;
        int col=0;

        for(Content c:list){
            if(row==65535) break;
            ws.addCell(new Label(0, row, c.getContentExt().getTitle()));
            ws.addCell(new Label(1, row, c.getAttr().get("name")));
            ws.addCell(new Label(2, row, c.getAttr().get("phone")));
            ws.addCell(new Label(3, row, c.getAttr().get("lawCode")));
            ws.addCell(new Label(4, row, c.getContentTxt().getTxt()));
            row++;

        }

    }
        wb.write();
        wb.close();

        os.close();
       response.flushBuffer();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RowsExceededException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (WriteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 悟空在此 2015-01-05 08:01

    在 os.close()前
    加个os.flush();

    评论
    解决 无用
    打赏 举报
  • 微风吹过的夏天2016 2015-01-05 14:01

    数据没设置到流中去。
    InputStream in = new FileInputStream(file);
    byte[] buf=new byte[1024];
    ServletOutputStream out=response.getOutputStream();
    int len=0;
    while((len=in.read(buf))!=-1){
    out.write(buf, 0, len);
    }
    in.close();
    out.flush();
    out.close();

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题